简体   繁体   中英

Editing multiple tag data in XML via batch

    (for /F "delims=" %%a in (Settings.xml) do (
       set "line1=%%a"
       set "newLine1=!line1:DefaultLatitude>=!"
       if "!newLine1!" neq "!line1!" (
          set "newLine1=<DefaultLocation>%newLoc%</DefaultLocation>"
       )
       echo !newLine1!
    )) > newFile.xml
    copy newFile.xml Settings.xml

I'm using this to modify the value of DefaultLocation in Settings.xml, how can I expand this to be able to modify multiple tags within the same function?

At the moment I'm just copy and pasting it and it seems inefficient

Use an inner loop with "from|to" values ( | looks good and is unlikely to be used in your values):

(for /F "delims=" %%a in (Settings.xml) do (
    set "line=%%a"
    for %%b in (
        "DefaultLatitude>|<DefaultLocation>%newLoc%</DefaultLocation>"
        "herp>|<foo>%foo%</foo>"
        "derp>|<bar>%bar%</bar>"
    ) do for /f "delims=| tokens=1,2" %%c in (%%b) do (
        if "!line:%%c=!" neq "!line!" set "line=%%d"
    )
    echo.!line!
)) > newFile.xml
  • echo. is used to correctly print empty lines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM