Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Find and Replace text in all text files in a specific folder - NT4 OS  (Read 21236 times)

0 Members and 2 Guests are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #30 on: July 07, 2015, 09:32:45 PM »
I'll be back to work on Thursday to test further at the system.

Your emulator of NT4 is probably correct to what you are experiencing. NT4 has been a pain for batches as well as other missing features within windows that are grayed out such as the defrag utility non functional, but it shows it there to tease you. I didnt really get involved with NT until Windows 2000 Pro and Server and as far as batches went, pretty much all prior methods of batch file executions worked fine with 2000 ( NT5 ). NT4 I remember being more of a pain for trying to find hardware that supported NT4 with drivers etc. Never did I ever expect to find that its very very picky with batches and features like %date% and %time% are missing among other features that batch on a system normally executes correctly on just about all other Microsoft OS's DOS/Windows based.

Thanks for your continued effort on this issue  :)

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #31 on: July 08, 2015, 07:32:54 AM »
I have a solution using Qbasic - which is built into the NT4 I have - as
NT4 looks to be quite retarded in way-of-the-batch-file.

A limitation in Qbasic is that it doesn't handle long filenames - which I can work around,
but I wanted to ask if there are long filenames on your server in this task.

Your version of NT4 may or may not have Qbasic.exe, I guess,
as Microsoft loved removing tools from different releases.

If you open a cmd prompt and type qbasic then it will either open or give an error message.


Here is the current batch file that uses qbasic, and handles short filenames.  I tested it here in NT4.

Code: [Select]
@echo off

dir /b /a-d *.mtx *.xts >tempfile.tmp

set "file=edit.bas"
 >"%file%" echo  OPEN "tempfile.tmp" FOR INPUT AS #3
>>"%file%" echo    DO WHILE NOT EOF(3)
>>"%file%" echo     LINE INPUT #3, file$
>>"%file%" echo      OPEN file$ FOR INPUT AS #1
>>"%file%" echo      OPEN "temp.txt" FOR OUTPUT AS #2
>>"%file%" echo        DO WHILE NOT EOF(1)
>>"%file%" echo          LINE INPUT #1, a$: y=1
>>"%file%" echo          if not INSTR(ucase$(a$), "LS: N"       ) = 0 THEN ? #2, "LS: Y"       : y=0
>>"%file%" echo          if not INSTR(ucase$(a$), "$LASTSORT N" ) = 0 THEN ? #2, "$LASTSORT Y" : y=0
>>"%file%" echo          if y=1 then ? #2, a$
>>"%file%" echo        LOOP
>>"%file%" echo     CLOSE #1, #2
>>"%file%" echo    kill file$
>>"%file%" echo    name "temp.txt" as file$
>>"%file%" echo   LOOP
>>"%file%" echo SYSTEM

qbasic /run %file%

del %file%
del tempfile.tmp
echo done
pause

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #32 on: July 08, 2015, 04:02:14 PM »
I will give this a try tomorrow. Its been years since I messed with QBasic, but hopefully its on the system to perform this work around for what batch can not handle alone with limitations of NT4.


File names are all short like

345sortrun2.mtx
345sortrun1.mtx


none show with a tilde to indicate long file names etc, so I think that this might be the final solution. Thanks for all your help with this  :)

patio

  • Moderator


  • Genius
  • Maud' Dib
  • Thanked: 1769
    • Yes
  • Experience: Beginner
  • OS: Windows 7
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #33 on: July 08, 2015, 04:14:13 PM »
Doggone titlde's ! !
" Anyone who goes to a psychiatrist should have his head examined. "

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #34 on: July 08, 2015, 05:35:50 PM »
File names are all short like

345sortrun2.mtx
345sortrun1.mtx
Those are long file names, and are not 8.3. command.com and cmd.exe do not show short file names by default. Use dir /X in command prompt to see the generated short file names. It's also possible to disable the short file name generation entirely- that would be troublesome I expect.
I was trying to dereference Null Pointers before it was cool.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #35 on: July 09, 2015, 08:40:13 AM »
I think some of my confusion with NT4 was due to mistakes I made.

This should work for your files - test it on samples of your file in an empty folder.
It works fine in the NT4 system here, and assumes qbasic is available on the path.

It handles long filenames - and due to a bug in the for command it will process some files twice
but of course not change anything further the second time. 
Unless you are processing stacks of files, the extra loops will make little difference.

Code: [Select]
@echo off
set "file=edit.bas"
 >"%file%" echo  OPEN "temp.tmp" FOR INPUT AS #1
>>"%file%" echo  OPEN "temp.txt" FOR OUTPUT AS #2
>>"%file%" echo   DO WHILE NOT EOF(1)
>>"%file%" echo    LINE INPUT #1, a$: y=1
>>"%file%" echo    if not INSTR(ucase$(a$), "LS: N"       ) = 0 THEN ? #2, "LS: Y"       : y=0
>>"%file%" echo    if not INSTR(ucase$(a$), "$LASTSORT N" ) = 0 THEN ? #2, "$LASTSORT Y" : y=0
>>"%file%" echo    if y=1 then ? #2, a$
>>"%file%" echo   LOOP
>>"%file%" echo CLOSE
>>"%file%" echo SYSTEM

for %%a in (*.mtx *.xts) do (
del temp.t?? 2>nul
ren "%%a" temp.tmp
qbasic /run %file%
ren temp.txt "%%a"
)

del temp.t?? 2>nul
del %file%
echo done
pause

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #36 on: July 09, 2015, 09:20:34 AM »
Thanks Foxidrive

I got lucky in that the system DOES have QBasic to support this alt method

I tested this out on some files and it worked perfect. I made some adjustments outside of this source that you provided since I decided that I wanted the batch/qbasic script to get copied from one location C:\batches\ to the working directory where these config files are located and run once in that location and then perform a clean up process to remove the batch file from the folder that should only contain the 2 config file types.

Found out that for some reason the cleanup process couldnt be placed at the end of this after the pause which to me makes no sense.

I simply had an erase *.bat instruction after the pause to remove the copy of the batch/qbasic script from the config file directory and it told me File Not Found ... Yet, it worked in removing the Batch File from this directory. To get rid of the File Not Found message, I ended up calling to another batch file back at the c:\batches\ called cleanup.bat with a START C:\Batches\cleanup.bat which when placed after the pause worked correctly without the File Not Found oddity.

Not quite sure where the File Not Found message was coming from when  the erase *.bat was successfully removing the batch file from the sort plan configuration directory... so I guess I discovered another NT4 oddity.

But as of right now, we have success!!!!!!!!!!!!!!!!!!!!   This will work perfect so that I myself no longer have to manually edit many files weekly as well as coworkers who dont know their way around computers can simply click on a shortcut on the desktop and it will process the changes to all files so that I no longer get panic phone calls that the config files are messed up and having to step them through the process to edit these over the phone from home.

Sometimes I think that CH should allow a way to send gifts to others for their help on problems, although its a not for profit and not allowed. I'd be willing to send you a gift card to go out to lunch or dinner on me if it were allowed.  ;D

Through this process though, especially the scripts involving qbasic I learned that you can create a qbasic script edit.bas from within a batch script appending its instructions to the .bas file from batch and call it with qbasic /run %file% which is pretty cool. Never seen this done before as for usually when I mix batch with other languages such as C++, I already have the C++ file compiled,and then I am calling the program in its exe form to start. But your method allows for a single file to do it all vs calling a outside file/program. C++ wouldnt be able to be created and run the same way though because the .cpp file created from appended batch writes would have to be compiled first before execution and so the use of QBasic is perfect for systems with QBasic on them vs having to have a C++ compiler on them.

If it wasnt for the extremely strict security on this machines system to NOT introduce data via digital media transfer means, but hand coding is allowed at the terminal, I would have just gone the easy route of using Perl or C++ to achieve the same goal that was needed which I am familiar with from past experience. But given the limitations, I figured batch should be plenty enough to do this, BUT its going beyond my batch knowledge. And due to NT4 being a pain as was initially expected when i was running into troubles trying to craft a batch on my own from web script references for find and replace of text in files, I knew that someone here at CH would be able to assist and I was thinking that the plug was going to get pulled on this because NT4 was being a serious pain, but you came up with a really sexy way of pulling it off with QBasic when NT4 was putting up roadblocks.

For the find and replace with C++ I would have read in all data from the config file up to the detection of the LS: N or $LASTSORT N and write this to a temp file. Then write appended LS: Y or $LASTSORT Y to this temp file depending on an IF condition like you have in yours, and then read in from original file all contents from the end of the LS: N or $LASTSORT N to EOF and append that data to the temp file, although to do this for all files, i would have probably cheated by using a SYSTEM(); call and performed a DIR *.* >directory.log and then accessed the directory.log to then step through all the files one by one running this process and in the end deleting the original file and then renaming the temp file as the original, until I got to the end of the directory.log listing of files. There may be a better method vs a system call using C++, but I havent gotten to that in depth with C++ yet to work staying strictly within C++ methods and not going outside of the cheating to run to system calls and tapping into DOS features and then read in from that into the C++ logic. I have a tendency to make programs that are down and dirty and not reinventing wheels by cheating to the use of system calls, although people in the past have also pointed out to me that in trying to avoid reinventing the wheel, I ended up reinventing the wheel using my system call method because for example with Perl there was a function I was unaware of that handled this within Perl and extremely well.  ;D

What you created was well thought out and professionally crafted imo  :)

Anyways I have babbled enough. Thanks so much to You and BC on the efforts to get this working for us  8)

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #37 on: July 09, 2015, 10:51:01 AM »
Thanks Foxidrive

I got lucky in that the system DOES have QBasic to support this alt method
I tested this out on some files and it worked perfect.

You're welcome.  I'm glad the system has qbasic, and your filenames must be 8.3 format else Qbasic would stop and whine.

Quote
I made some adjustments outside of this source
I simply had an erase *.bat instruction after the pause to remove the copy of the batch/qbasic script from the config file directory and it told me File Not Found ...

This is a known issue with all batch files - you need to use various tricks to make the batch file delete itself AND not display the error message.
The message is harmless, but you do see it without special tricks.

Quote
This will work perfect so that I myself no longer have to manually edit many files weekly as well as coworkers who dont know their way around computers can simply click on a shortcut on the desktop and it will process the changes to all files so that I no longer get panic phone calls that the config files are messed up and having to step them through the process to edit these over the phone from home.

Brilliant.  That's the beauty of automating stuff. 

WRT C++ and batch scripts: in modern Windows there are compilers for c# and a batch script can create the source code in a text file, much like the qbasic script, and the batch file can then compile the exe file, use it, and even delete the exe file as it cleans up. 

I'm not skilled in c# or c++ but doing things that way can give enormous speed boosts for certain types of operations, where batch code is sluggish. 

Quote
Sometimes I think that CH should allow a way to send gifts to others for their help on problems, although its a not for profit and not allowed. I'd be willing to send you a gift card to go out to lunch or dinner on me if it were allowed.  ;D

That's nice of you to say so - and I think many people who assist others on CH and elsewhere get their enjoyment from scripting, helping people, and in the interaction - plus in learning things along the way. 

Quote
If it wasnt for the extremely strict security on this machines system to NOT introduce data via digital media transfer means, but hand coding is allowed at the terminal, I would have just gone the easy route of using Perl or C++ to achieve the same goal that was needed which I am familiar with from past experience.

The restriction is obviously place to avoid malware etc.

It's not cheating using system calls, in my view anyway, as the only drawback would be a loss of speed if they were in a tight/inner loop. 
In assembler/machine code it was common to call routines in the BIOS and that's as much a system call as anything else.  All above board to me. :)

Quote
Thanks so much to You and BC on the efforts to get this working for us  8)

You're welcome, and thanks to BC it went past the stage where a lack of a test machine would have stymied further work. 

BTW, you may have been posting and not seen another post above yours - it handles long filenames in that one.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #38 on: July 09, 2015, 11:41:32 AM »
Quote
BTW, you may have been posting and not seen another post above yours - it handles long filenames in that one.

You are correct... I thought the file names exceeded 8.3, but when checking back this morning vs from personal memory, they actually were 8 characters each, with none of then exceeding 8 characters so I ran with what you posted prior. Sorry for making extra work by posting examples from personal memory that were in excess of 8 characters. I took the initial code out to the system printed out and typed it all in and tested it and then added other instructions for it to perform at the beginning to copy a copy of the batch/qbasic script to the working directory, run it there, and end of it to perform a clean up which I had to call to a different batch to erase the batch file. The cool thing now is that there are examples of code for future visitors for handling on 8.3 and long file names for similar text replacement needs.

Quote
This is a known issue with all batch files - you need to use various tricks to make the batch file delete itself AND not display the error message.
The message is harmless, but you do see it without special tricks.

Yeah I tried the simple trick of hiding the message behind a CLS clear screen to refresh after the error message and then printing via echo that the process is complete since otherwise it was doing as it was instructed which was to delete itself, and it still showed File Not Found and so I decided to go the route of calling another batch to perform the cleanup vs the script itself cleaning up after itself. I couldnt find a way to place the clear screen between the error message and the last echo to user, its as if any error conditions are always displayed before the echo out to user and no getting around this from within itself so calling the other clean up batch was the solution there I guess.