Making use of the technique in tip 7723 » How can I prevent the FOR command from stripping leading spaces,
and CMPCTRL.bat, CMDCTRLdo.vbs, and CMDCTRLundo.vbs,
I have scripted RepTxt.bat to replace, delete, and insert text into a file.
I created RepTxt.bat so that I would not have to constantly write a new script to make mass changes to the pages on this site.
The syntax for using RepTxt.bat is:
RepTxt iFile rFile
Where iFile is the fully qualified file name of the file you wish to modify, and rFile is the fully qualified file name of the control file.
The rFile control file has commands, in order of precedence. The command syntax is \[cc\]string, where string is the search/replace/insertion text, and cc is:
\[ST\] - Search for the string. \[RT\] - Replace the string specified in the preceding \[ST\] commands with this string. A null replacement string is allowed. \[FB\] - Delete the line that contains the string, through the line that contains the string specified in the following \[FE\] command. If you only wish to delete one line, only use the \[FE\] command. \[FE\] - Delete the line that contains the string and enable insertion. If a preceding \[FB\] command exists, delete all lines in the \[FB\] through \[FE\] range. \[IB\] - If the preceding command is an \[FE\] or \[IC\], insert a line with the string. Multiple \[IB\] commands are allowed. \[IE\] - If insertion is enabled, insert a line with the string and terminate insertion. \[IC\] - Enable insertion after the line that contains the string.NOTE: If you need multiple insertion sets, it is better to run RepTxt.bat multiple times, with different control files.
RepTxt.bat contains:
@echo off setlocal if exist "%TEMP%\RepTxt_rFile.tmp" del /q /f "%TEMP%\RepTxt_rFile.tmp" if exist "%TEMP%\RepTxt_iFile.tmp" del /q /f "%TEMP%\RepTxt_iFile.tmp" if exist "%TEMP%\RepTxt_xFile.tmp" del /q /f "%TEMP%\RepTxt_xFile.tmp" if \{%2\}==\{\} goto err1 if not exist %1 goto err2 if not exist %2 goto err3 set iFile=%1 set rFile=%2 set VALOK=Y set pCode=\[NO\] set pLine=\[NO\] set bypass=*#$\[RepTxt_BYPASS\]$#* set insert=*#$\[RepTxt_INSERT\]$#* call CMDCTRL %rFile% "%TEMP%\RepTxt_rFile.tmp" for /f "Tokens=*" %%r in ('type "%TEMP%\RepTxt_rFile.tmp"') do ( set rLine=%%r call :VALrLine ) if "%VALOK%" EQU "N" goto finish call CMDCTRL %iFile% "%TEMP%\RepTxt_iFile.tmp" for /f "Tokens=1* Delims=:" %%a in ('type "%TEMP%\RepTxt_iFile.tmp"^|Findstr /l /n /v /c:"*$"') do ( set iLine=%%b call :REPiLine ) if "%VALOK%" EQU "N" goto finish del /f /q %iFile% call CMDCTRL "%TEMP%\RepTxt_xFile.tmp" %iFile% /U del /q /f "%TEMP%\RepTxt_xFile.tmp" :finish if exist "%TEMP%\RepTxt_rFile.tmp" del /q /f "%TEMP%\RepTxt_rFile.tmp" if exist "%TEMP%\RepTxt_iFile.tmp" del /q /f "%TEMP%\RepTxt_iFile.tmp" endlocal goto :EOF :REPiLine if "%iLine%" EQU "" goto REPiLO set FND=N for /f "Tokens=*" %%r in ('type "%TEMP%\RepTxt_rFile.tmp"') do ( set rLine=%%r call :find ) if "%iLine%" EQU "%bypass%" goto :EOF if "%iLine%" EQU "" goto REPiLO ( @echo %iLine% )>>"%TEMP%\RepTxt_xFile.tmp" goto :EOF :REPiLO ( @echo. )>>"%TEMP%\RepTxt_xFile.tmp" goto :EOF :find if "%FND%" EQU "Y" goto findY if "%iLine%" EQU "%bypass%" goto :EOF if /i "%rLine:~0,4%" EQU "\[ST\]" goto RST if /i "%rLine:~0,4%" EQU "\[RT\]" goto :EOF if /i "%rLine:~0,4%" EQU "\[FB\]" goto RFB if /i "%rLine:~0,4%" EQU "\[FE\]" goto RFE if /i "%rLine:~0,4%" EQU "\[IB\]" goto :EOF if /i "%rLine:~0,4%" EQU "\[IC\]" goto :RIC if /i "%rLine:~0,4%" EQU "\[IE\]" goto :EOF :errl @echo RepTxt - Invalid RepTxt logic. set VALOK=N endlocal goto :EOF :findY if /i "%rLine:~0,4%" EQU "\[ST\]" goto :EOF if /i "%rLine:~0,4%" EQU "\[RT\]" goto RRT if /i "%rLine:~0,4%" EQU "\[FB\]" goto :EOF if /i "%rLine:~0,4%" EQU "\[FE\]" goto RFE if /i "%rLine:~0,4%" EQU "\[IB\]" goto RIB if /i "%rLine:~0,4%" EQU "\[IC\]" goto :RIC if /i "%rLine:~0,4%" EQU "\[IE\]" goto RIE goto errl :RST if "%iLine%" EQU "%bypass%" goto :EOF call set ans=%%iLine:%rLine:~4%=%% if "%ans%" EQU "%iLine%" goto :EOF set FND=Y set pLine=%rLine% goto :EOF :RRT if "%iLine%" EQU "%bypass%" goto :EOF if /i "%pLine:~0,4%" NEQ "\[ST\]" goto :EOF call set iLine=%%iLine:%pLine:~4%=%rLine:~4%%% set FND=N set pLine=%rLine% goto :EOF :RFB if "%iLine%" EQU "%bypass%" goto :EOF call set ans=%%iLine:%rLine:~4%=%% if "%ans%" EQU "%iLine%" goto :EOF set FND=Y set iLine=%bypass% set pLine=%rLine% goto :EOF :RFE if "%iLine%" EQU "%bypass%" goto :EOF call set ans=%%iLine:%rLine:~4%=%% if "%ans%" EQU "%iLine%" goto :RFEX set FND=Y set iLine=%bypass% set pLine=%rLine% goto :EOF :RFEX if /i "%pLine:~0,4%" NEQ "\[FB\]" goto :EOF set iLine=%bypass% goto :EOF :RIB if /i "%pLine:~0,4%" EQU "\[IC\]" goto RIBX if /i "%pLine:~0,4%" NEQ "\[FE\]" goto :EOF :RIBX if "%rLine:~4%" EQU "" goto RIX ( @echo %rLine:~4% )>>"%TEMP%\RepTxt_xFile.tmp" goto :EOF :RIC if "%iLine%" EQU "%bypass%" goto :EOF call set ans=%%iLine:%rLine:~4%=%% if "%ans%" EQU "%iLine%" goto :EOF set FND=Y set pLine=%rLine% if "%iLine%" EQU "" goto RICX ( @echo %iLine% )>>"%TEMP%\RepTxt_xFile.tmp" set iLine=%bypass% goto :EOF :RICX ( @echo. )>>"%TEMP%\RepTxt_xFile.tmp" set iLine=%bypass% goto :EOF :RIE if /i "%pLine:~0,4%" EQU "\[IC\]" goto RIEX if /i "%pLine:~0,4%" NEQ "\[FE\]" goto :EOF :RIEX set FND=N set pLine=%rLine% if "%rLine:~4%" EQU "" goto RIX ( @echo %rLine:~4% )>>"%TEMP%\RepTxt_xFile.tmp" goto :EOF :RIX ( @echo. )>>"%TEMP%\RepTxt_xFile.tmp" goto :EOF :VALrLine if /i "%rLine:~0,4%" EQU "\[ST\]" goto VST if /i "%rLine:~0,4%" EQU "\[RT\]" goto VRT if /i "%rLine:~0,4%" EQU "\[FB\]" goto VFB if /i "%rLine:~0,4%" EQU "\[FE\]" goto VFE if /i "%rLine:~0,4%" EQU "\[IB\]" goto VIB if /i "%rLine:~0,4%" EQU "\[IC\]" goto VIC if /i "%rLine:~0,4%" EQU "\[IE\]" goto VIE :errx @echo RepTxt - Invalid rFile RepTxtment coding. set VALOK=N endlocal goto :EOF :VALe set pCode=%rLine:~0,4% goto :EOF :VST if /i "%pCode%" EQU "\[ST\]" goto errx if /i "%pCode%" EQU "\[IB\]" goto errx if /i "%pCode%" EQU "\[FB\]" goto errx if /i "%pCode%" EQU "\[IC\]" goto errx goto VALe :VRT if /i "%pCode%" EQU "\[ST\]" goto VALe goto errx :VFB if /i "%pCode%" EQU "\[ST\]" goto errx if /i "%pCode%" EQU "\[IB\]" goto errx goto VALe :VFE if /i "%pCode%" EQU "\[FB\]" goto VALe if /i "%pCode%" EQU "\[ST\]" goto errx if /i "%pCode%" EQU "\[IB\]" goto errx goto VALe :VIB if /i "%pCode%" EQU "\[IB\]" goto VALe if /i "%pCode%" EQU "\[IC\]" goto VALe if /i "%pCode%" NEQ "\[FE\]" goto errx goto VALe :VIC if /i "%pCode%" EQU "\[ST\]" goto errx if /i "%pCode%" EQU "\[IB\]" goto errx if /i "%pCode%" EQU "\[FB\]" goto errx goto VALe :VIE if /i "%pCode%" EQU "\[IB\]" goto VALe if /i "%pCode%" EQU "\[IC\]" goto VALe if /i "%pCode%" NEQ "\[FE\]" goto errx goto VALe :err1 @echo Syntax: RepTxt iFile rFile endlocal goto :EOF :err2 @echo RepTxt - %1 NOT found. endlocal goto :EOF :err3 @echo RepTxt - %2 NOT found. endlocal