Find & Replace in a file using DOS.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
yaduvanshi123
Posts: 4
Joined: 02 May 2012 04:03

Find & Replace in a file using DOS.

#1 Post by yaduvanshi123 » 02 May 2012 04:44

Hi Team,

I need the script/code by which I can do the find & replace in the file & save it with different name using command prompt. For example :

I have the source file "RUN.bat" with the contents

:: *** ACSD -
:: del "%A%"\ACSD_error
:: del "%Ap%"\ACSD_audit

Here I would like to replace all the ACSD with XYZ and create the new file "New.bat" witht he contents

:: *** XYZ -
:: del "%A%"\XYZ_error
:: del "%Ap%"\XYZ_audit

Regards,
Yaduvanshi.


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace in a file using DOS.

#3 Post by foxidrive » 02 May 2012 08:36

Untested:


@echo off
call sar.bat "run.bat" "new.bat" "ASCD" "XYZ"



Code: Select all

:: sar.bat
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs

yaduvanshi123
Posts: 4
Joined: 02 May 2012 04:03

Re: Find & Replace in a file using DOS.

#4 Post by yaduvanshi123 » 02 May 2012 23:14

foxidrive wrote:Untested:


@echo off
call sar.bat "run.bat" "new.bat" "ASCD" "XYZ"



Code: Select all

:: sar.bat
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs


I am getting the error " 'cscript' is not recognized as an internal or external command, operable program or batch file."

Please suggest.

Regards,
Yaduvanshi.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace in a file using DOS.

#5 Post by foxidrive » 03 May 2012 09:20

What version of Windows are you using? Has the PATH variable been modified by you?

I have this in XP:

c:\WINDOWS\system32\cscript.exe

yaduvanshi123
Posts: 4
Joined: 02 May 2012 04:03

Re: Find & Replace in a file using DOS.

#6 Post by yaduvanshi123 » 04 May 2012 06:11

Tha path is same for me as well...I have checked.
I am using Windows7 Enterprise Edition 32 bit OS.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find & Replace in a file using DOS.

#7 Post by foxidrive » 04 May 2012 17:24

open a cmd prompt and type

Code: Select all

cscript /?



See what you get. It is possible that you need to run it as Admin.

Post Reply