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 21283 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Trying to come up with a batch that will work with NT4 OS and find and replace specific lines in text files. There are about 60 of these text files that hold operational parameters in them. Up to this point on a weekly basis when they are updated on sundays at 7am, I have been just going in with notepad and changing the specific lines from N to Y to enable features and then ALT+F+S and ALT+F+X which i can get through 60 files pretty quickly manually changing specific lines to Y from N.

The problem now is that I am going on vacation in the near future and I am hoping I can leave for a coworker a batch that can automatically change the value from N to Y in all files in a specific directory where it finds the lines of

LS: N    (or)     $LASTSORT N

which would be changed to

LS: Y  (or)    $LASTSORT Y

The find instruction in the FOR loop would be best written to test all files by * wild cards for file name and file extension at a specific directory path that is not at the root of C: but in a path such as C:\Machine1\Parameters\Sorts\ in which the file name and extension are not altered, but it goes down a list in the directory to test all files for these specific lines and if they are set to N switch them to Y and then save the changes to the files by a write to file instruction.

The location might not be on the same exact lines within these config files for the machine parameter files, so its not as easy as just targeting a specific file extension file and knowing to just without checking just forcing say line 12 to be set to LS: Y for a xts file extension     (or)     $LASTSORT Y at line 8 in all files with mtx file extension.

So because the lines in which these parameters can be different from one operational config file to the next, each file has to be searched for the location of the line that needs to be altered and then when detected altering that line only.

I was messing around with the batches found here and saw that Foxidrive worked on this project at stack overflow. http://stackoverflow.com/questions/23087463/batch-script-to-find-and-replace-a-string-in-text-file-within-a-minute-for-files

But it uses a winscript that is called in a repl.bat file outside of the batch shown and I am not sure if NT4 supports that, so I am thinking that a pure native batch might be necessary like this one, but for all files to be tested and altered if specific lines are found. I also dont care about how long it takes as this other person at stack overflow wanted it to run faster for a 12MB file. My files are like 37k in size so it should run pretty quick anyways even if not efficient in how it carries it out.


Code: [Select]
@echo off &setlocal
set "search=%1"
set "replace=%2"
set "textfile=Input.txt"
set "newfile=Output.txt"
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
del %textfile%
rename %newfile%  %textfile%

Lost with how to apply to all files by wild card and retain the original file name, and a batch that would work with limitations of NT4 OS which I have found in the past to not support all modern batch methods that work on NT5 and newer. I also dont have a NT4 system to test against currently while at home, but I do have an NT4 OEM Workstation CD and original COA in storage that I might be able to create a VM for to then test the batch against. And use this guide for Virtual PC 2007 that I have used in the past and am familiar with running virtual environments in which i can run on my Windows XP Pro SP2 tinker system to test against. http://www.essjae.com/virtualization/winvpc-installing_winnt4.pdf

At some point I need to learn to use VMWare I suppose and get away from VPC 2007  :P

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Have you seen this?
http://stackoverflow.com/questions/833293/how-to-run-vbscript-in-windows-nt-4
OR
http://www.serverwatch.com/tutorials/article.php/1548191/Using-ADSI-and-VBScript-to-Access-Objects-in-NT4-and-the-Active-Directory.htm
Also, early version of QBASIC will run on NT.
QBASIC has a nice set of string operations. But the free version ov ABSIC did not have a good command line option set. So you have to send your parameters to a file and then have QBASIC read the parameters from a file.




foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
If the lines are exactly what you have shown, without extra whitespace or text, then give this a test:

Blank lines will disappear, if that is an issue.

Code: [Select]
@echo off
cd "c:\folder"
for %%a in (*.txt) do (
(
   for /f "delims=" %%b in ('type "%%a"') do (
      if "%%b"=="LS: N" echo LS: Y
      if "%%b"=="$LASTSORT N" echo $LASTSORT Y
      if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
   )
)>"tempnewfile.tmp"

del "%%a"
ren "tempnewfile.tmp"  "%%a"
)

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Thanks Foxidrive for this batch to try... I was thinking i was going to need to use an escape character before the $ in $LASTSORT where you have

Quote
     if "%%b"=="$LASTSORT N" echo $LASTSORT Y
     if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b

but cool that batch doesnt need an escape character for the $ outside of a string " " area as I highlighted that your echo-ing out. Learned something new.  :)

Also reading the batch you provided it looks like its looking only in *.txt text files, so I would need to change the file extension to the extension of the target which would be one FOR loop for the *.xts and the another FOR loop for the *.mtx file extension to test when all *.xts files have been tested for the string match and replace if found. As I altered what you gave me below, or should it be best rewritten with an OR statement in a single FOR loop to test both for the string match in both *.xts or *.mtx files or even just change to a total wild card of a single FOR loop testing all files with *.*? or would a *.* cause a CRC Error due to it grabbing at the tempnewfile.tmp file which is also in the target directory of execution and has bitten me before with xcopy instructions with wild cards and writing back to source drive.


Code: [Select]
@echo off
cd "c:\folder"
for %%a in (*.xts) do (
(
   for /f "delims=" %%b in ('type "%%a"') do (
      if "%%b"=="LS: N" echo LS: Y
      if "%%b"=="$LASTSORT N" echo $LASTSORT Y
      if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
   )
)>"tempnewfile.tmp"

del "%%a"
ren "tempnewfile.tmp"  "%%a"
)

for %%a in (*.mtx) do (
(
   for /f "delims=" %%b in ('type "%%a"') do (
      if "%%b"=="LS: N" echo LS: Y
      if "%%b"=="$LASTSORT N" echo $LASTSORT Y
      if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
   )
)>"tempnewfile.tmp"

del "%%a"
ren "tempnewfile.tmp"  "%%a"
)


( OR )

If there is no risk of a CRC Error of it grabbing at its own temp file use a *.* to test all at the location by which this is executed, where the batch is executed from a location say at C: which then the instructions of the batch switch the target to C:\folder as you have on the 2nd line.
Code: [Select]
@echo off
cd "c:\folder"
for %%a in (*.*) do (
(
   for /f "delims=" %%b in ('type "%%a"') do (
      if "%%b"=="LS: N" echo LS: Y
      if "%%b"=="$LASTSORT N" echo $LASTSORT Y
      if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
   )
)>"tempnewfile.tmp"

del "%%a"
ren "tempnewfile.tmp"  "%%a"
)

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
My original code has a problem - due to a bug in the for command - and adding an extension as I described below will also fix that issue.

You can process different filetypes using a single for loop and the ren command keeps the original extension
but adds an extra one - and that extra one is removed after all files are processed, by the last ren command.



Code: [Select]
@echo off
cd "c:\folder"
for %%a in (*.mtx *.xts) do (
(
   for /f "delims=" %%b in ('type "%%a"') do (
      if "%%b"=="LS: N" echo LS: Y
      if "%%b"=="$LASTSORT N" echo $LASTSORT Y
      if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
   )
)>"tempnewfile.tmp"
del "%%a"
ren "tempnewfile.tmp"  "%%a.final"
)
ren *.final *.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Thanks Foxidrive ... I am going to try this out today on the PC of the system. Going to make a backup copy of the files first just in case, but looks like it should work as intended.

Didnt know that a FOR loop could have  ( *.mtx *.xts ) like this. I was thinking that for a single FOR loop it might need to be 2 separate arguments like an OR condition such as ( *.mtx ) || ( *.xts ) otherwise it would take *.mtx *.xts as a single string vs testing for one or the other file types.

Learned something new.  :)

Will let you know how it went in a reply back later. Thanks for your help on this!  8)

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Just tested it and it looks like maybe there is an issue with this batch and NT4 not supporting it or some other issue.

Command Shell Window goes black for about 10 seconds as its processing and then scrolls duplicate file found and then gets to the pause that I added in the batch so that the command shell window wouldnt close and I could see the messages.

All but 4 config files at the C:\Machine1\Parameters\Sorts\ location for *.mtx and *. xts are changed to 0k files with contents wiped out and the 4 that weren't completely wiped out of all other parameters had LS: Y in the 1k files. The Files are normally like 37k in size and have all sorts of config parameters in them. I ended up reverting back from backup of the files so that I can try the batch again when a correction is made. In the folder where this is all happening there are 2 copies of each file with 2 different file extensions, but as stated above all but 4 files are wiped out of data and 0k in size.

I have run into other difficulties with batch and NT4 so this wouldnt be the first time that a batch that should work and probably works fine on a newer NT based OS pukes on NT4. The last time I ran into issues with NT4 it was using the date and time calls which NT4 doesnt like %date% and %time%. I didnt add anything else to the batch you provided, I just changed the path as necessary for the target and added a pause at the end to see the last messages before close of the window as highlighted below.


Quote
@echo off
cd "C:\Machine1\Parameters\Sorts\"
for %%a in (*.mtx *.xts) do (
(
   for /f "delims=" %%b in ('type "%%a"') do (
      if "%%b"=="LS: N" echo LS: Y
      if "%%b"=="$LASTSORT N" echo $LASTSORT Y
      if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
   )
)>"tempnewfile.tmp"
del "%%a"
ren "tempnewfile.tmp"  "%%a.final"
)
ren *.final *.
pause

Thank You for your efforts on this and hopefully it doesnt make you want to pull your hair out like I have felt like doing with NT4 and batches in the past. The -NT4 OS on the subject line of this was to make it clear that its NT4 OS and is probably going to be picky with what it supports for instructions. My guess is that this batch either has a small typo that I am not seeing or needs to be tweaked to a legacy format that NT4 is fine with.  :-\

EDIT: Was just thinking could there be a character in these config files that is causing the batch to go haywire such as these characters as for the config files are loaded with config info that uses special characters and HEX etc such as these characters !@#$%^&*(){}[]~`';:"<>,.?/\|+=-_     Maybe I'm off the beaten path and its something else, but I have run into issues before with other scripts and the script hitting characters that cause issues, so figured I'd touch on this if its a concern or not. Maybe it doesnt matter what info is in the config files as for its only looking for the specific matches and no other characters can foul it up.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Can you try this batch script?  It works on W2K - and just creates a test folder and
creates two files to test it with.

See if any errors occur before the first pause and what they are.

I don't have NT4 to test it with here.

Code: [Select]
@echo off
md "C:\Machine1\Parameters\Sorts\testdave"
cd "C:\Machine1\Parameters\Sorts\testdave"

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.mtx

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.xts

echo after file creation
pause

for %%a in (*.mtx *.xts) do (
(
   for /f "delims=" %%b in ('type "%%a"') do (
      if "%%b"=="LS: N" echo LS: Y
      if "%%b"=="$LASTSORT N" echo $LASTSORT Y
      if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
   )
)>"tempnewfile.tmp"
del "%%a"
ren "tempnewfile.tmp"  "%%a.final"
)
echo before final rename
ren *.final *.
pause

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
I'm not dave but I do have NT4. Not sure how useful that will be. I ran the batch file and it works without any error up to the first pause. After continuing I get a few errors:

Code: [Select]
Press any key to continue . . .
A duplicate file name exists, or the file
cannot be found.
before final rename
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
Press any key to continue . . .

I was trying to dereference Null Pointers before it was cool.

foxidrive



    Specialist
  • Thanked: 268
  • Experience: Experienced
  • OS: Windows 8
Thanks BC_Programmer, that is useful.
I thought I'd traced a flaw but my brain must be out-to-lunch, as it was the same code.

If you have time to test this then it may help locate the problem.

Run this in an empty folder.


Code: [Select]
@echo off
:md "C:\Machine1\Parameters\Sorts\testdave"
:cd "C:\Machine1\Parameters\Sorts\testdave"

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.mtx

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.xts

echo after file creation
dir
pause

   for %%a in (*.mtx *.xts) do (
 (
      for /f "delims=" %%b in ('type "%%a"') do (
         if "%%b"=="LS: N" echo LS: Y
         if "%%b"=="$LASTSORT N" echo $LASTSORT Y
         if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
      )
  )>"tempnewfile.tmp"
 del "%%a"
 ren "tempnewfile.tmp"  "%%a.final"
)


echo ===========
dir
echo before final rename
ren *.final *.
pause

If that fails can you try this version please?

Code: [Select]
@echo off
:md "C:\Machine1\Parameters\Sorts\testdave"
:cd "C:\Machine1\Parameters\Sorts\testdave"

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.mtx

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.xts

del "tempnewfile.tmp" 2>nul

   for %%a in (*.mtx *.xts) do (
      for /f "delims=" %%b in ('type "%%a"') do (
         if "%%b"=="LS: N" >>"tempnewfile.tmp" echo LS: Y
         if "%%b"=="$LASTSORT N" >>"tempnewfile.tmp" echo $LASTSORT Y
         if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" >>"tempnewfile.tmp" echo %%b
      )
 del "%%a"
 ren "tempnewfile.tmp"  "%%a.final"
)

ren *.final *.
pause

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 #10 on: June 26, 2015, 05:35:32 AM »
First Batch Output:

Code: [Select]
C:\test2>test
after file creation
 Volume in drive C has no label.
 Volume Serial Number is C875-5A5F

 Directory of C:\test2

06/26/15  03:22a        <DIR>          .
06/26/15  03:22a        <DIR>          ..
06/26/15  03:22a                    36 aaa.mtx
06/26/15  03:22a                    36 aaa.xts
06/26/15  03:20a                   693 test.bat
               5 File(s)            765 bytes
                          3,810,073,600 bytes free
Press any key to continue . . .
ECHO is off.
 Volume in drive C has no label.
 Volume Serial Number is C875-5A5F

 Directory of C:\test2

06/26/15  03:22a        <DIR>          .
06/26/15  03:22a        <DIR>          ..
06/26/15  03:22a                     7 aaa.mtx.final
06/26/15  03:22a                     7 aaa.xts.final
06/26/15  03:20a                   693 test.bat
               5 File(s)            707 bytes
                          3,810,073,600 bytes free
before final rename
A duplicate file name exists, or the file
cannot be found.
Press any key to continue . . .



Second batch:

Code: [Select]
C:\test2>test
The system cannot find the file specified.
Could Not Find C:\test2\aaa.mtx
The system cannot find the file specified.
Could Not Find C:\test2\aaa.mtx
Could Not Find C:\test2\aaa.mtx
The system cannot find the file specified.
The system cannot find the file specified.
Could Not Find C:\test2\aaa.xts
The system cannot find the file specified.
Could Not Find C:\test2\aaa.xts
Could Not Find C:\test2\aaa.xts
The system cannot find the file specified.
A duplicate file name exists, or the file
cannot be found.
Press any key to continue . . .

After the second batch file ran there were two files in the same folder as the batch file, "aaa" and "aaa.xts.final"


I did some tests myself here and compared with Windows 2000, and I think the difference (or perhaps one of many) may be that in NT4, rename appears to work differently. with two files test.xts.final and test2.xts.final, for example, on Windows 2000 (and presumably later) ren *.final *. will rename them to test.xts and test2.xts respectively. However, on NT4 it renames them to test and test2- removing the extension entirely.


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 #11 on: June 26, 2015, 06:17:48 AM »
Thanks again BC_Programmer, it is that final rename - a change as Windows versions flew past.

This may work - touch wood.  It relies on the filenames NOT having a plus + sign in them.

Code: [Select]
@echo off
:md "C:\Machine1\Parameters\Sorts\testdave"
:cd "C:\Machine1\Parameters\Sorts\testdave"

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.mtx

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.xts

   for %%a in (*.mtx *.xts) do (
 (
      for /f "delims=" %%b in ('type "%%a"') do (
         if "%%b"=="LS: N" echo LS: Y
         if "%%b"=="$LASTSORT N" echo $LASTSORT Y
         if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b
      )
  )>"%%a+tmp"
 del "%%a"
)

echo  ===== before rename & dir & echo ===== before rename & pause

for %%a in ("*+tmp") do (
   for /f "delims=+" %%b in ("%%~a") do ren "%%~a" "%%b"
)
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 #12 on: June 26, 2015, 06:27:04 AM »
Thanks BC to providing the NT4 environment testing for Foxidrive on this issue.

Will be checking back here periodically on when you want me to test on the live system.

Thanks for both of your help on this.

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 #13 on: June 26, 2015, 06:44:55 AM »
No error this time:

Code: [Select]
===== before rename
 Volume in drive C has no label.
 Volume Serial Number is C875-5A5F

 Directory of C:\testdata

06/26/15  04:41a        <DIR>          .
06/26/15  04:41a        <DIR>          ..
06/26/15  04:41a                     7 aaa.mtx+tmp
06/26/15  04:41a                     7 aaa.xts+tmp
06/26/15  04:39a                   710 test.bat
               5 File(s)            724 bytes
                          3,810,054,656 bytes free
===== before rename
Press any key to continue . . .
done
Press any key to continue . . .

The files don't seem to get renamed at the last step, though- both aaa.mtx+tmp and aaa.xts+tmp remain.
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 #14 on: June 26, 2015, 07:33:53 AM »
No error this time:

The files don't seem to get renamed at the last step, though- both aaa.mtx+tmp and aaa.xts+tmp remain.

Hmm - can you please show us what this prints to the console?

Code: [Select]
@echo off

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.mtx+tmp

(
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.xts+tmp

for %%a in ("*+tmp") do (
   echo "%%~a"
   for /f "tokens=1 delims=+" %%b in ("%%~a") do echo ren "%%~a" "%%b"
)
echo done
pause