-1

We have multiple batch scripts (around 200) that are being executed from a scheduler. We want to replace a command used in all batch scripts by a different command. Is there any command or utility that Windows provides for bulk editing of the files. (Something similar to Sed and AWK in unix).

Kindly help. Sample command is as given below: we need to replace Command Startscen by startscen_dev which is present in every script.

From:

startscen "PKG_TEST" "-1" "TEST" "-AGENT_URL=http://localhost:20910/oraclediagent"

To:

startscen_dev "PKG_TEST" "-1" "TEST" "-AGENT_URL=http://localhost:20910/oraclediagent"

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112

1 Answers1

0

The following code will replace startscen with startscen_dev in all .bat files in the folder specified. This may take a while for larger files. The script was based on this post. Also note that this will not replace the whole string, but only startscen as i couldn't get the quotes inside the quotes to work.

@echo off
REM The following variable is all you need to care about.
set targetFolder=folder\*.bat
for /r %%i in (%targetFolder%) do (
    call :FindReplace "startscen " "startscen_dev " "%%i"
)
exit

:FindReplace <findstr> <replstr> <file>
set tmp="%temp%\tmp.txt"
If not exist %temp%\_.vbs call :MakeReplace
for /f "tokens=*" %%a in ('dir "%3" /s /b /a-d /on') do (
  for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
    echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
    <%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
    if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
  )
)
del %temp%\_.vbs
exit

:MakeReplace
>%temp%\_.vbs echo with Wscript
>>%temp%\_.vbs echo set args=.arguments
>>%temp%\_.vbs echo .StdOut.Write _
>>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
>>%temp%\_.vbs echo end with
Community
  • 1
  • 1
Script_Coded
  • 709
  • 8
  • 21