0

I am trying to create a .bat script that goes into each subfolder in a folder and replaces a value in an .xml file based on the subfolders name. I believe I am quite close with the following:

@echo off
for /d %%i in ("E:\working\test\tmp\*") do (
    set "replaced=different"
    setlocal enableDelayedExpansion
    cd /d "%%i\word\"

    @echo on
    for /f "tokens=* delims= " %%a in (document.xml) do (
        set "line=%%a"
        set "line=!line:placeholder=%replaced%!"
        echo !line!>>new.txt

    )
)
endlocal
pause

The code above produces the result:

document.xml contents= "testplaceholdertest"
new.txt contents = "testtest"

I think the issue is with this line:

set "line=!line:placeholder=%replaced%!"

I don't think it is setting or using the %replaced% variable correctly, I could use %%~ni but I need to modify the name a little before replacing, hence why I need a variable to work. Anyone know why or how I would fix it?

aschipfl
  • 31,767
  • 12
  • 51
  • 89
Angus
  • 15
  • 4
  • Since variable `replaced` is set within the loop structure you would need delayed expansion too, but you cannot nest delayed expansion like `!line:placeholder=!replaced!!`, so you need another layer of expansion: 1. `for /F "delims=" %%r in (""!replaced!"") do set "line=!line:placeholder=%%~r!"` (preferable); 2. `call set "line=%%line:placeholder=!replaced!%%"`. Or you just set the variable before the loop structure… – aschipfl Sep 29 '21 at 08:22

0 Answers0