0

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
Blainer
  • 2,552
  • 10
  • 32
  • 39

1 Answers1

0

As far as I know, there is no inplace option for Windows Batch. I would just use a temporary file:

call :processFile < template.html > tmp_file
move tmp_file template.html

If Unix/Linux user are interested in an answer:

There is Colin Watson's sponge (packed for Debian in the moreutils package):

theConvertProgram < template.html | sponge template.html

See https://unix.stackexchange.com/a/29744/15241 for more information.

Solution with temporary file:

theConvertProgram < template.html > tmp_file
mv tmp_file template.html

improved version with mktemp:

TEMP_FILE="$(mktemp)"
theConvertProgram < template.html >  "$TEMP_FILE"
mv "$TEMP_FILE" template.html
Community
  • 1
  • 1
jofel
  • 3,297
  • 17
  • 31
  • this method will work, but how would you write it to automatically execute on all files in the directory with a .html extension instead of "template.html" specifically? – Blainer Mar 20 '12 at 16:05
  • would this work? `call :processFile < *.html > tmp_file` `move tmp_file *.html` – Blainer Mar 20 '12 at 16:21
  • @Blainer This would not work, you need a for-loop: `for %%f in (*.html) do ( call :processFile < %%f > tmp_file ; move tmp_file %%f )` , see http://stackoverflow.com/a/39664/1182783 for more examples. – jofel Mar 20 '12 at 16:30