How would I change the below script to edit the original file instead of generating a new one? Also, how would I make this script execute on all .html files in the same directory instead of having to specify one file at a time.
@echo off
setlocal EnableDelayedExpansion
call :processFile < template.html > final.html
goto :EOF
:processFile
set line=EOF
set /P line=
if "!line!" == "EOF" goto :EOF
set X=
set "lineNoGen=!line:Gen_1_=$!"
if "!lineNoGen!" neq "!line!" (
for /F "tokens=1-3 delims=$" %%a in ("!lineNoGen:Gen.1.=$!") do (
set "beforeGen=%%a"
set "betweenGens=%%b"
set "afterGen=%%c"
set "X=!betweenGens:~0,1!"
set /A Xm1=X-1, Xp1=X+1
echo !beforeGen!Gen_1_!Xm1!!betweenGens:~1!Gen.1.!Xm1!!afterGen:~1!
)
)
echo !line!
if defined X (
echo !beforeGen!Gen_1_!Xp1!!betweenGens:~1!Gen.1.!Xp1!!afterGen:~1!
)
goto :processFile
As far as I know, there is no inplace option for Windows Batch.
I would just use a temporary file:
If Unix/Linux user are interested in an answer:
There is Colin Watson’s
sponge(packed for Debian in themoreutilspackage):See https://unix.stackexchange.com/a/29744/15241 for more information.
Solution with temporary file:
improved version with
mktemp: