The batch file described at
http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace has limitations. If the text file to be searched/replaced contains on of the characters
&|<> in a line without being quoted then the batch will not process this line correctly. (A proper note has been added to the post.)
The following script acts opposite. It only works if the special characters
&|<> are quoted if they appear (rafaelbc this may help in your case):
Code: Select all
@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
if "%*"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1* delims=]" %%A in ('type %3^|find /n /v ""') do (
set "line=%%B"
if defined line (
call echo.%%line:%~1=%~2%%
) else (echo.)
)
Output:
C:\>type BatchSubstitute.txt
!!! THIS TEST DOESN`T REPLACE ANYTHING, IT JUST CHECKS MAKES SURE
!!! THE SCRIPT WORKS FOR SPECIAL CHARACTERS
!!! THE SCRIPT DOESN'T WORK CORRECTLY IF ANY OF THE CHARACTERs "&|^"
!!! APPEAR IN A LINE WITHOUT BEING QUOTED.
Next line is empty
this ( is a opening bracket character
this ) is a closing bracket character
this % is a percent character
this ! is an exclamation character
this " is a quote character
this > is a grater-than character
this < is a less-than character
this & is an ampersand character
this | is a vertical character
this ^ is a up character
this is a "quoted string"
this is a %percented string%
this is a !exclamationed string!
a "quoted ( opening bracket"
a "quoted ) closing bracket"
a "quoted % quote character"
a "quoted ! quote character"
a "quoted " quote character"
a "quoted > grater-than bracket"
a "quoted < less-than bracket"
a "quoted & ampersand"
a "quoted | vertical"
a "quoted ^ up character"
C:\>BatchSubstitute2.bat Oldtext Newtext BatchSubstitute.txt
!!! THIS TEST DOESN`T REPLACE ANYTHING, IT JUST CHECKS MAKES SURE
!!! THE SCRIPT WORKS FOR SPECIAL CHARACTERS
!!! THE SCRIPT DOESN'T WORK CORRECTLY IF ANY OF THE CHARACTERs "&|^"
!!! APPEAR IN A LINE WITHOUT BEING QUOTED.
Next line is empty
this ( is a opening bracket character
this ) is a closing bracket character
this % is a percent character
this ! is an exclamation character
this " is a quote character
this a grater-than character
this a less-than character
this is a up character
this is a "quoted string"
this is a %percented string%
this is a !exclamationed string!
a "quoted ( opening bracket"
a "quoted ) closing bracket"
a "quoted % quote character"
a "quoted ! quote character"
a "quoted " quote character"
a "quoted > grater-than bracket"
a "quoted < less-than bracket"
a "quoted & ampersand"
a "quoted | vertical"
a "quoted ^ up character"
If however you have a mixed case, where sometimes the special character
&|<> appear within quotes and not within quotes, then there seems to be no clean batch file solution for a search and replace.