3

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
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2
    Are you ***really*** using "MS-DOS" or are you referring to the command line in Windows? –  Sep 15 '16 at 14:19
  • The script you are using is not designed to use a search string which inclues an **=** character. To test the script replace the search string with `set search=2222 bbbb` and the replace as `set replace=$%RANDOM%` – Compo Sep 15 '16 at 14:36
  • Can you use Vbscript? – Squashman Sep 15 '16 at 17:48
  • 1
    A little, simple searching would go a long way on this. How about? http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – lit Sep 15 '16 at 22:09
  • Possible duplicate of [Batch file : how to search and replace a string that have an "=" inside](http://stackoverflow.com/questions/37724410/batch-file-how-to-search-and-replace-a-string-that-have-an-inside) – aschipfl Sep 15 '16 at 22:28
  • 1
    Do you want to replace always whole lines, or also partial strings? And something else: you should not use the name `replace` for a batch file as there is also a command named like this... – aschipfl Sep 15 '16 at 22:40

3 Answers3

1

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
::*************************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

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
elzooilogico
  • 1,659
  • 2
  • 17
  • 18
0

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
Compo
  • 36,585
  • 5
  • 27
  • 39