Solved How can you find and replace text in a file using the batch?

January 18, 2013 at 07:32:50
Specs: Windows XP
Hi all.
My text file with contens here:
_____________________
line 1
line 2
...
<show>true</show>
line xx
<hide>true</hide>
_____________________

Now. I want replace string <show>true</show> to <show>false</show>to
And i want get to text file:
_____________________
line 1
line 2
...
<show>false</show>
line xx
<hide>true</hide>
_____________________

Thanks



See More: How can you find and replace text in a file using the batch?

Report •

#1
January 18, 2013 at 18:21:07
✔ Best Answer
Well, since you spec'd batch, I'll give an approximation, which may or may not work (batch is not well suited to html or xml):
::===== begin batchscript
@echo off & setlocal
for /f "tokens=*" %%a in (test1) do call :aa "%%a"
goto :eof

:aa
set z=%1
set z=%z:<show>true</show>=<show>false</show>%
for /f "tokens=*" %%b in (%z%) do >>test2 echo %%~b
::===== end batch
Note this creates a second file but does not wipe out the original (ie: safety is in effect)
Here's a vbscript, which I recommend over batch, for handling this type of file:
'====== begin vbscript
set fso=createobject("scripting.filesystemobject")
z=replace(lcase(fso.opentextfile("test1",1).readall),"<show>true</show>","<show>false</show>")
fso.createtextfile("test2").write z
'======= end vbscript
I glommed a bunch of steps together (vbs). If you want an un-glommed version, let me know (it is much easier to follow when it is portioned out).


Report •

#2
January 18, 2013 at 19:44:15
Wow.
I very like your batch code.
Thank you very much.

Report •
Related Solutions


Ask Question