computing
  • 5

Solved Batch File The Will Check Xml File String And Replace Value

  • 5

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

Share

1 Answer

  1. 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

    • 1