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

0 Members and 1 Guest are viewing this topic.

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 »