Home / Microsoft / Microsoft DOS / Find and Replace one string with another inside the file using DOS batch
0 Members and 3 Guests are viewing this topic. « previous next »
Pages: 1 [2]  All - (Bottom) Print
Author Topic: Find and Replace one string with another inside the file using DOS batch  (Read 24280 times)
Raven19528
Hopeful



Thanked: 29
Posts: 284

Computer: Specs
Experience: Experienced
OS: Windows 7



« Reply #15 on: September 29, 2011, 10:10:38 AM »

Wow, this thread just keeps getting revived from the grave. Fun.

There is a way to do this in batch, though there are much better tools out there to accomplish the task as shown throughout the thread. The batch method is outlined and not fully coded below:

This command would be used for one file and would need to be embedded in another for command if multiple files were needing to be changed.
for /f "delims=" %%G in (changefile.txt) do (
  set oldstring=%%G
  set newstring=!oldstring:~0,  52=0,  21!
  echo !newstring!>>temp.txt
)
del changefile.txt
ren temp.txt changefile.txt


Again, this could be run on multiple files using this for command embedded into another for command that would determine what is placed in the "changefile.txt" area. Obviously you could put multiple "set newstring=" commands in if there were multiple changes that were needing to be made.

Can we let this thread die now? It has lived a long life and should be allowed to rest in peace.
 ::)
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
Raven19528
Hopeful



Thanked: 29
Posts: 284

Computer: Specs
Experience: Experienced
OS: Windows 7



« Reply #16 on: September 30, 2011, 11:02:16 AM »

for /f "delims=" %%G in (changefile.txt) do (
  set oldstring=%%G
  set newstring=!oldstring:~0,  52=0,  21!
  echo !newstring!>>temp.txt
)
del changefile.txt
ren temp.txt changefile.txt


One error: there is no ~ after the colon in the set newstring line. It should read:
set newstring=!oldstring:0,  52=0,  21!
IP logged

"All things that are
Are with more spirit chased than enjoy'd" -Shakespeare
Pages: 1 [2]  All - (Top) Print 
Home / Microsoft / Microsoft DOS / Find and Replace one string with another inside the file using DOS batch « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.081 seconds with 19 queries.