Thanks @Aacini.
That took care of the !s.
I had to play around with blank lines. I had to make sure the template.xml had at least a space on an otherwise blank line (otherwise it would be skipped), and to trim blanks and check for an empty line, I ended up with
Code: Select all
@ECHO Off
setlocal DisableDelayedExpansion
SET MyVar=%1
(for /F "delims=" %%G in (%CD%\MyTemplate.xml) do (
set "str=%%G"
setlocal EnableDelayedExpansion
set "strTrim={x}!str!{x}"
if "!strTrim!"=="{x}{x}" (
CALL :BlankLine
) ELSE (
set "str=!str:MyVar=%MyVar%!"
IF "!str!"=="" (
CALL :BlankLine
) ELSE (
set "strTrim=!strTrim: ={x}!"
set "strTrim=!strTrim: ={x}!"
set "strTrim=!strTrim:{x}{x}={x}!"
set "strTrim=!strTrim:{x}{x}={x}!"
:: I had to repeat the set "strTrim=!strTrim:{x}{x}={x}!" several times to get rid of all combinations of blanks - I could also have edited the template, of course...
IF "!strTrim!"=="{x}" (CALL :BlankLine) ELSE (ECHO !str!)
)
)
endlocal
)) > %CD%\newfile.xml
goto :eof
:BlankLine
ECHO(
goto :eof
As an intermediate attempt, I had simply turned my XML template into a batch file with every line formatted as
ECHO ^<... %MyVar% ...^> >> %CD%\newfile.xml
(with the substitution variables inline where needed)
and blank lines as
ECHO( >> %CD%\newfile.xml
Ugly, but since the XML layout is static, it works, too...
But I like yours better!