Find and Replace a string of a file in all subfolders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
autonicknan
Posts: 19
Joined: 01 Mar 2013 11:44

Find and Replace a string of a file in all subfolders

#1 Post by autonicknan » 01 Mar 2013 12:06

:| :| Hello Everybody,

I need to implement a batch script that will access a file called kaka.txt and will replace the string DELAY(30*SEC) with DELAY(50*SEC).
This file(kaka.txt) is at about under 100 subfolders. I need this modification to be applied at any kaka.txt file under these subfolders.

Up to now and with the help of other posts I have done this:

=========================================================
echo off

for /R C:\example\ %%G IN (kaka.txt) DO (

call C:\example\BatchSubstitute.bat "DELAY(30*SEC)" DELAY(50*SEC) %%G>new.txt

)
echo
pause

==========================================================

unfortunately this does not work.

The BatchSubtitude.bat is batch file that finds and replace the DELAY(30*SEC) with DELAY(50*SEC) copied from another post of this forum :)
When I use it without the FOR loop it works but just for one kaka.txt file in one of the subfolders.

The C:\example has 100 subfolders that contain the kaka.txt


Does anybody know what is wrong with my FOR loop?

Thanks
Nikos

autonicknan
Posts: 19
Joined: 01 Mar 2013 11:44

Re: Find and Replace a string of a file in all subfolders

#2 Post by autonicknan » 01 Mar 2013 13:21

Hi again,

Well after several tries I found out that the DO does not work when the calling command is in parenthesis () and moreover I noticed that in order to have some result the whole code concerning the FOR loop must be in one individual line like this:

for /R C:\example\ %%G IN (kaka.txt) DO call C:\example\BatchSubstitute.bat "DELAY(30*SEC)" DELAY(50*SEC) %%G>new.txt


Apart from that the problem that I do face now is that just one new.txt file is generated under the path C:\example and none in the subfolders that I am interested for.

Any idea?

Thanks,
Nikos

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find and Replace a string of a file in all subfolders

#3 Post by Squashman » 01 Mar 2013 14:32

Code: Select all

for /R C:\example\ %%G IN (kaka.txt) DO call C:\example\BatchSubstitute.bat "DELAY(30*SEC)" DELAY(50*SEC) %%G>%%~dpGnew.txt

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

Re: Find and Replace a string of a file in all subfolders

#4 Post by foxidrive » 01 Mar 2013 22:26

This works here. The source string needs to have some characters escaped with \ in the regular expression.

Code: Select all

echo off
set "file=kaka.txt"
pushd "C:\example"
for /f "delims=" %%a in (' dir "%file%" /s /b /a-d ') do (
pushd "%%~dpa"
echo found "%%a"
call :replace "%file%" "temp.tmp" "DELAY\(30\*SEC\)" "DELAY(50*SEC)"
move /y "temp.tmp" "%file%" >nul
popd
)
popd
pause
goto :EOF

:replace
::Search and replace
@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

autonicknan
Posts: 19
Joined: 01 Mar 2013 11:44

Re: Find and Replace a string of a file in all subfolders

#5 Post by autonicknan » 02 Mar 2013 02:04

Thank you very much both of you.

Both answers were very helpful!!

Post Reply