DOS Batch - Find and Replace

Search a file and replace all occurrences of a string with another string.

Description:

This batch allows string substitution in a text file. It parses each line of a text file for a particular string and replaces it with another string.
I.e. To replace all occurrences of "Yellow Submarine" in "color.txt" with "uboot" and put the output on the screen run:
BatchSubstitute.bat "Yellow Submarine" uboot color.txt
Or
type color.txt|BatchSubstitute.bat "Yellow Submarine" uboot

Optionally pipe the output into a new file, i.e.
BatchSubstitute.bat "Yellow Submarine" uboot color.txt>newfile.txt
Or
type color.txt|BatchSubstitute.bat "Yellow Submarine" uboot>newfile.txt

Note: Due to the nature of the FOR command and string substitution, the following known restrictions apply:
  • Lines starting with "]" character will end up empty
  • OldStr must not start with "*"
  • Lines must not contain any of the following characters within a quoted string: "&<>|^", as discussed in this post.
Script: Download: BatchSubstitute.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
@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
:$changed 20100115
:$source https://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%~1=%~2%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.
)
Script Output:
 DOS Script Output
C:>type BatchSubstitute.txt
!!! THIS TEST DOESN`T REPLACE ANYTHING, IT JUST MAKES SURE
!!! THE SCRIPT WORKS FOR SPECIAL CHARACTERS.
!!! THE SCRIPT WOULD NOT WORK IF ANY OF THE CHARACTERs &<>|^ WOULD
!!! APPEAR WITHIN A QUOTED STRING
Next line is empty

this > is a grater-than character
this < is a less-than character
this ( is a opening bracket character
this ) is a closing bracket character
this % is a percent character
this & is an ampersand character
this ! is an exclamation character
this " is a quote 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"

C:>BatchSubstitute.bat Oldtext Newtext BatchSubstitute.txt
!!! THIS TEST DOESN`T REPLACE ANYTHING, IT JUST MAKES SURE
!!! THE SCRIPT WORKS FOR SPECIAL CHARACTERS.
!!! THE SCRIPT WOULD NOT WORK IF ANY OF THE CHARACTERs &<>|^ WOULD
!!! APPEAR WITHIN A QUOTED STRING
Next line is empty

this > is a grater-than character
this < is a less-than character
this ( is a opening bracket character
this ) is a closing bracket character
this % is a percent character
this & is an ampersand character
this ! is an exclamation character
this " is a quote 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"