user3918517
user3918517

Reputation: 31

Find and Replace string in a text file

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

Upvotes: 3

Views: 1793

Answers (3)

Hackoo
Hackoo

Reputation: 18827

If you want a little trick with a vbscript like this one :

@echo off
set "Data=some data .... $cc = 3333"
echo The data before replacing is "%Data%" & pause
set "String1=$cc = 3333"
set "String2=cc = 4444" 
Call :ReplaceString "%Data%" "%String1%" "%String2%"
echo The data after replacing is "%Data%" & pause & exit
::*************************************************************************************
:ReplaceString <Data> <String1> <String2>
(
    echo Wscript.echo Replace("%~1","%~2","%~3"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "Data=%%a" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::*************************************************************************************

EDIT : On 15/09/2016 @ 19:30

@echo off
Set "InputFile=%~dp0InputFile.txt"
Set "OutputFile=%~dp0OutputFile.txt"
set "String1=$cc = 3333"
set "String2=cc = 4444" 
if exist "%OutputFile%" Del "%OutputFile%"
Setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('Type "%InputFile%"') do (
    If /I "%%i" == "%String1%" (
        Call :ReplaceString "%%i" "%String1%" "%String2%"
        echo !Data!
        ) else (
        echo %%i
    )
)>> "%OutputFile%"
start "" "%OutputFile%" & exit
::*************************************************************************************
:ReplaceString <Data> <String1> <String2>
(
    echo Wscript.echo Replace("%~1","%~2","%~3"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "Data=%%a" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::*************************************************************************************

Upvotes: 1

Compo
Compo

Reputation: 38579

Just change your for loop:

@echo off

set "search=$cc = 3333"
set "replace=cc = 4444"
set "textfile=InputFile.txt"
set "newfile=OutputFile.txt"

>"%newfile%" (for /f "delims=" %%i in (%textfile%) do (
        If /I "%%i" Equ "%search%" (Echo=%replace%) Else (Echo=%%i)))
exit/B

Upvotes: 0

elzooilogico
elzooilogico

Reputation: 1705

We need an expert here! Surely he/she knows how to do this.

Substituting a string that contains = sign is tricky. As far as I know, you must split in two and test for both terms or, if string to search is fixed, try

@echo off

set "search=$cc = 3333"
set "replace=cc = 4444"
set "textfile=InputFile.txt"
set "newfile=OutputFile.txt"

>"%newfile%" (
  for /f "delims=" %%i in (%textfile%) do (
    if "%%i" equ "%search%" (
      echo(%replace%
    ) else (
      echo(%%i
    )       
  )
)
exit/B

Upvotes: 0

Related Questions