Replace a string

March 6, 2010 at 20:42:17
Specs: Windows 7
HI,
is it possible for a batch file to find a string in a .txt file and then replace it with another string
I.e a file named myfile.txt has:
hello
cd
qwerty

and changed to:

hello
dfg
qwerty.

Thank you in advance


See More: Replace a string

Report •


#1
March 6, 2010 at 20:58:02
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (myfile.txt) do (
set S=%%a
set S=!S:cd=dfg!
>> newfile echo.!S!
)
move /y newfile myfile.txt


=====================================
Helping others achieve escape felicity

M2


Report •

#2
March 7, 2010 at 02:56:35
If you download some unix-like tools (sed.exe), you could do this:

D:\Temp>type test.txt
This is a test
Just another line
And why not, one more

D:\Temp>sed "s/Just another/My modified/g" test.txt
This is a test
My modified line
And why not, one more

D:\Temp>


Report •

#3
March 7, 2010 at 19:07:30
Thank you to both of you ,tvc and Mechanix2Go.
Both of them are really helpful

Report •
Related Solutions


Ask Question