I have to replace string in a text file (InputFile.txt
) using Windows command/batch scripting. I found following script (replace.cmd
) but it is not giving me accurate result.
InputFile.txt
:
1111 aaaa
2222 bbbb
$cc = 3333
The batch script (replace.cmd
) that I am using to replace $cc = 3333
to cc = 4444
is:
@echo OFF
setlocal enabledelayedexpansion
set "search=$cc = 3333"
set "replace=cc = 4444"
set "textfile=InputFile.txt"
set "newfile=OutputFile.txt"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
The output what I am getting after running the code is:
1111 aaaa
2222 bbbb
3333=cc = 4444= 3333
It should be something like:
1111 aaaa
2222 bbbb
cc = 4444