Solved batch file the will check xml file string and replace value

December 19, 2012 at 18:02:10
Specs: Windows 7
Need a batch file the will check my xml file and replace value of a string. hope you can help


@echo off
setlocal EnableDelayedExpansion
set "_search=value1"
set "_replace=A"
set "_search=value2"
set "_replace=B"

for /F "delims=" %%a in (LastestFRN.xml) DO (
set line=%%a
setlocal EnableDelayedExpansion
>> LastestFRN3.xml echo(!line:%_search%=%_replace%!
endlocal
)
del LastestFRN.xml
rename LastestFRN3.xml LastestFRN.xml


See More: batch file the will check xml file string and replace value

Report •


#1
December 20, 2012 at 18:27:40
✔ Best Answer
Well, vbscript is not batch, but it can handle your requirements SO much better:
'========begin vbscript
set fso=createobject("scripting.filesystemobject")
content=fso.opentextfile("n.xml",1).readall
content=replace(content,wscript.arguments(0),wscript.arguments(1))
content=replace(content,wscript.arguments(2),wscript.arguments(3))
fso.opentextfile("n.xml",2).write content
'======= end vbscript

Note that the xml filename is hardwired, but that is easily remedied by shifting the arguments and putting the filename as argument 0. Also, no error-handling is in place, so missing arguments etc. will terminate the procedure.

And here's my re-take on the code you posted:

@echo off
setlocal EnableDelayedExpansion
set _search=value1
set _replace=A

for /F "delims=" %%a in (LastestFRN.xml) DO (
set line=%%a
>> LastestFRN3.xml echo(!line:%_search%=%_replace%!
)
move /y LastestFRN3.xml LastestFRN.xml


Report •
Related Solutions


Ask Question