I found the function "substitute" appears to be a simple c&p from the sample script "BatchSubstitute.bat". Afaics, the latter should work as expected (I did not try), but the function does not work as desired. First, it misses ENABLEEXTENSIONS, and secondly, it does only output the new file contents on stdout. So I fixed it to do as desired:
Code: Select all
:substitute OldStr NewStr File -- substitutes a string in a text file
:: -- OldStr [in] - string to be replaced
:: -- NewStr [in] - string to replace with
:: -- File [in] - file to be parsed
:$created 20060101 :$changed 20150121 :$categories FileManipulation
:$source http://www.dostips.com
SETLOCAL DISABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v ""&&BREAK>%3"') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X>>%3
) ELSE echo.>>%3
)
EXIT /b
This works, at least on my Win7.