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
6
  • 2
    Are you really using "MS-DOS" or are you referring to the command line in Windows? Commented Sep 15, 2016 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% Commented Sep 15, 2016 at 14:36
  • Can you use Vbscript? Commented Sep 15, 2016 at 17:48
  • 1
    A little, simple searching would go a long way on this. How about? stackoverflow.com/questions/60034/… Commented Sep 15, 2016 at 22:09
  • 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... Commented Sep 15, 2016 at 22:40

3 Answers 3

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
::*************************************************************************************
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.