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 21163 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

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 #15 on: June 26, 2015, 07:47:15 AM »
Code: [Select]
C:\testdata>test
""
""
done
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
Re: Find and Replace text in all text files in a specific folder - NT4 OS
« Reply #16 on: June 26, 2015, 09:12:37 AM »
I should say that I've never used NT4 - it appears as though the tilda contructs
like %%~nxa are not supported in NT4, or the the service pack you have, BC_Programmer.

Thanks for your help in this growing thread. :)

Does this show the filenames as I think it should?  They should be double quoted on the console too.

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

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 #17 on: June 26, 2015, 10:30:56 AM »
Output from that batch:
Code: [Select]
C:\testdata>test
aaa.mtx+tmp
ren aaa.mtx+tmp "$LASTSORT N"
ren aaa.mtx+tmp "PL120"
ren aaa.mtx+tmp "LS: N"
ren aaa.mtx+tmp "dostips"
aaa.xts+tmp
ren aaa.xts+tmp "$LASTSORT N"
ren aaa.xts+tmp "PL120"
ren aaa.xts+tmp "LS: N"
ren aaa.xts+tmp "dostips"
done
Press any key to continue . . .

I'm testing with Service Pack 6a.
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 #18 on: June 26, 2015, 09:04:46 PM »
Oops, it's parsing the file.

This will either work, or it wont.  :D

Thanks for your testing - it shows how many times a script needs to be tweaked sometimes.


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 (
   for /f "tokens=1 delims=+" %%b in ("%%a") do echo ren "%%a" "%%b"
)
echo done
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 #19 on: June 26, 2015, 11:52:31 PM »
Code: [Select]
C:\testdata>test
ren "aaa.mtx+tmp" "aaa.mtx"
ren "aaa.xts+tmp" "aaa.xts"
done
Press any key to continue . . .

Looks good, I think.
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 #20 on: June 27, 2015, 12:24:24 AM »
Looks good, I think.

Excellent, thanks.

I stitched together the snippets - hopefully correctly.

Code: [Select]
@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
      )
  )>"%%a+tmp"
 del "%%a"
)

for %%a in ("*+tmp") do (
   for /f "tokens=1 delims=+" %%b in ("%%a") do echo 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 #21 on: June 27, 2015, 03:48:36 PM »
I'll give this a try. Thanks to both of you for extensive efforts on getting this to work.  8)

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 #22 on: July 05, 2015, 12:25:40 PM »
Sorry for the delay in getting back to this, but just got back to work after vacation and tested this at the system and here is what it does...

All config files that have a match for LS: N or $LASTSORT N get overwritten with just a LS: Y or $LASTSORT Y which end up being 1kb files with only that information in them.

So instead of this example of a config file being:

Quote
$D $SXC 87:3
GxvS a06593ef01bc
:hsxv 435.67
:gcxv 598.98
offset -32.6
x axis: 9857.76
y axis: 482.53
z axis: 1025.92
LS: N
( followed by about 500 more lines of config data )

Changing to

Quote
$D $SXC 87:3
GxvS a06593ef01bc
:hsxv 435.67
:gcxv 598.98
offset -32.6
x axis: 9857.76
y axis: 482.53
z axis: 1025.92
LS: Y
( followed by about 500 more lines of config data )

It changes to a 1kb file with just

Quote
LS: Y

with all other config data gone.  :-\


Files that do not have a match to LS: N or $LASTSORT N get overwritten with a 0kb file with no contents, where it should be skipping over these since no alteration is needed on these where a match is not found in the config contents.

The files with the match to $LASTSORT N end up the same as the LS: N matched files in which you end up with 1kb files for those files that have a match to $LASTSORT N which it writes $LASTSORT Y to the file and wipes out all other config data, as well as the files without a match to $LASTSORT N or LS: N end up getting wiped out of config contents and becoming 0kb files.  :-\

Question for BC_Programmer, when you tested this on your NT4 SP6 machine did you test with other data in the text file to see if it overwrites ( wipes out ) your data just like I saw here with 1kb and 0kb files or is there something different between your NT4 SP6 and our machine with the computer running it to where its only malfunctioning at my end?   Thanks for checking on this and responding.  :)


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 #23 on: July 05, 2015, 02:10:25 PM »
Quote
Question for BC_Programmer, when you tested this on your NT4 SP6 machine did you test with other data in the text file to see if it overwrites ( wipes out ) your data just like I saw here with 1kb and 0kb files or is there something different between your NT4 SP6 and our machine with the computer running it to where its only malfunctioning at my end?   Thanks for checking on this and responding.

Did I test with other data in the text file? No. I didn't create any text files. I just ran the batch files and provided the output with no real presumptions about preconditions.
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 #24 on: July 05, 2015, 10:22:28 PM »
Sorry for the delay in getting back to this, but just got back to work after vacation and tested this at the system and here is what it does...

All config files that have a match for LS: N or $LASTSORT N get overwritten with just a LS: Y or $LASTSORT Y which end up being 1kb files with only that information in them.

If the files appear to only have "LS: Y" in them and are 1KB in filesize, then they have other characters in them that are invisible - because "LS: Y" and a carriage return and line feed is only 7 bytes.

My last post had an echo in the rename command, and so the files should have had the wrong extension.
It's unclear if you fixed that, or the script you used was different in some way.

Can you test the bottom script here on the machine in an empty folder?
The top script creates some test files - so run that first and verify that the files look ok, and then run the script that is below that to see what happens to the test files.

If that works, then we'd need one of your files to see what is inside them - perhaps it has NULL characters or some lines are over 2KB in length - which  I believe is the limit of line lengths that a script can process in NT.

Code: [Select]
@echo off

(
echo $D $SXC 87:3
echo GxvS a06593ef01bc
echo :hsxv 435.67
echo :gcxv 598.98
echo offset -32.6
echo x axis: 9857.76
echo y axis: 482.53
echo z axis: 1025.92
echo LS: N
echo dostips
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.mtx

(
echo $D $SXC 87:3
echo GxvS a06593ef01bc
echo :hsxv 435.67
echo :gcxv 598.98
echo offset -32.6
echo x axis: 9857.76
echo y axis: 482.53
echo z axis: 1025.92
echo LS: N
echo dostips
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>aaa.xts

(
echo $D $SXC 87:3
echo GxvS a06593ef01bc
echo :hsxv 435.67
echo :gcxv 598.98
echo offset -32.6
echo x axis: 9857.76
echo y axis: 482.53
echo z axis: 1025.92
echo LS: N
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>bbbaaa.mtx

(
echo $D $SXC 87:3
echo GxvS a06593ef01bc
echo :hsxv 435.67
echo :gcxv 598.98
echo offset -32.6
echo x axis: 9857.76
echo y axis: 482.53
echo z axis: 1025.92
echo LS: N
echo $LASTSORT N
echo PL120
echo LS: N
echo dostips
)>bbbaaa.xts

Code: [Select]
@echo off

rem 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
      )
  )>"%%a+tmp"
 del "%%a"
)

for %%a in ("*+tmp") do (
   for /f "tokens=1 delims=+" %%b in ("%%a") do ren "%%a" "%%b"
)
echo done
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 #25 on: July 05, 2015, 10:33:21 PM »
If the files appear to only have "LS: Y" in them and are 1KB in filesize
He may be looking at Windows Explorer, which would round up to 1K.

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

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 #26 on: July 06, 2015, 08:53:14 AM »
Quote
He may be looking at Windows Explorer, which would round up to 1K.
BC is correct... that is what I was looking at, so its probably rounded to 1k.

I will test this out at the system and get back to you with results.

Quote
My last post had an echo in the rename command, and so the files should have had the wrong extension.
It's unclear if you fixed that, or the script you used was different in some way.

Yes... I did see this minor issue. If I am only dealing with wrong file extensions as a minor problem... that could be easily corrected for.  :)

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 #27 on: July 06, 2015, 11:47:51 AM »
ok sorry for the delay in getting back on this... had to squeeze this test in during my normal maintenance window with this machine.

I took some screenshots to show what I have and what I am getting. Also in the screenshots I placed the directory listing so you can see the exact file sizes of before and after.

I performed the test that you requested and got the same results. I then decided to make a smaller file with regular text in it and no special characters or formatting and got the same results as seen in the 2 screenshots.

Sorry that the screenshots are scrunched. I had to fit bit maps onto floppy disks, and this lead to 30 minutes having to dig up a healthy floppy drive to install into the system and then find some floppies that didnt get eaten from grime and dust bunnies. That one floppy drive that I did find that wasnt too dirty, I am going to keep in a baggie some place safe with a could floppies for in the future when i need to get data off of the system or back it up ever again.

As far as implementing the batch file on this system, I had to print out the info you provided and manually key it all in and confirm multiple times checking that I dont have a typo between what you have listed and what I typed. I used notepad to make the batch file and save as a .bat file. Reason for not copy/pasting info from this computer from CH thread to notepad and then saved to a floppy with batch file on it is that data security is "VERY TIGHT".... so i have the right to take a blank floppy and copy "non sensitive" data to it to share from this machine, but I am not allowed to bring data to it in data file format by any media and so I have to manually type in the batch file to implement it in which no virus/malware/backdoor hack tools can travel unless intentionally coded in notepad..  ;D

For the fact that its wiping out the contents on both file types and only the LS: N containing file is replaced with LS: Y and the $LASTSORT N file is always a 0 sized file lacking replacement to $LASTSORT Y, we might be looking at 2 problems. If it was just 1 problem then the contents would be wiped out and the file would be populated with $LASTSORT Y in place of $LASTSORT N

Hopefully your not getting frustrated with this and enjoy challenges that NT4 seem to pose for myself and now all who are involved on CH working on this trying to figure it out. Many thanks in all efforts on trying to come to a working solution on this if one could be made given the complications.

NT4 and batch files for me has been very frustrating. Also today when trying to perform an unconditional format on a floppy disk I found out that NT4 doesnt have an /u unconditional format switch to try to get part of the floppy disk to work in the drive for a damaged disk.  ::)

Well here is the screenshots and here is an exact copy/paste of my code that I typed off of yours direct from the machine through floppy sneaker network.  :P
Code: [Select]
@echo off
rem 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
)
)>"%%a+tmp"
del "%%a"
)

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

This batch was executed from C:\Machine1\Parameters\Sorts


Pic1 is before the batch is run the file contents.

Pic2 is after the batch is run the file contents wiped out and only the file containing LS: N replaced with LS: Y  ???

The good thing is that the file extensions are correct now!  ;D




[attachment deleted by admin to conserve space]

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 #28 on: July 07, 2015, 02:22:14 AM »
You should be able to take a batch script in via floppy - it can't contain a virus -
but anyway, your script works here so it was fine.

In the script below I've removed the fancy method of echoing to a file, as it's likely that is a problem there.
All you can do is to continue testing - but I will try to source an NT emulator to test it here.

EDIT: I recalled that NT (and maybe W2K) required the comparison operators like EQU LEQ and also
the goto :EOF to be in upper case.  I changed the "not" keywords to "NOT" just in case that is also an issue.

Code: [Select]
@echo off

rem cd "C:\Machine1\Parameters\Sorts"

for %%a in (*.mtx *.xts) do (

      for /f "delims=" %%b in ('type "%%a"') do (
         >>"%%a+tmp" if "%%b"=="LS: N" echo LS: Y
         >>"%%a+tmp" if "%%b"=="$LASTSORT N" echo $LASTSORT Y
         >>"%%a+tmp" if NOT "%%b"=="LS: N" if NOT "%%b"=="$LASTSORT N" echo %%b
      )

 del "%%a"
)

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

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 #29 on: July 07, 2015, 04:25:36 AM »
Quick update: I stumbled across an NT 4 system, and I can't believe what I am seeing.

"delims=" doesn't work - it needs to be set to something.

Image 1: Later: In the top image it is looking like %%b is only evaluated once within a loop.
                        If another attempt to evaluate %%b is made then the following one (same as #1) doesn't work.

Image 2: More laterer: This shows that the two identical lines work when %%b isn't compared in a different way.  How bizarre.

...or maybe Virtualbox broke the NT emulation.

[attachment deleted by admin to conserve space]
« Last Edit: July 07, 2015, 05:12:40 AM by foxidrive »

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.