Replace text in bat files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dukdpk
Posts: 13
Joined: 10 Mar 2011 03:27

Replace text in bat files

#1 Post by dukdpk » 10 Mar 2011 03:58

Hi All

I have set of bat files. all bat file contain 'TEST_2011' text. I want to replace TEST_2011 with TEST_2012. My bat files names are r1.bat,r2.bat,r3.bat and r4.bat. Thing is I want to replace new text in all bat files at once. I tried following bat file to replace new text into bat files. But Still I can replace only in r1.bat. I expect replace new text into all bat files which are r1.bat,r2.bat,r3.bat and r4.bat. Please advise me how to fulfill my requirement.

@echo off >newr1.bat & setLocal enableDELAYedexpansion
set /p old=old string ?
set /p new=new string ?

for /f "tokens=* delims= " %%a in (r1.bat) do (
set str=%%a
set str=!str:%old%=%new%!

>> newr1.bat echo !str!
)






Thank you

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Replace text in bat files

#2 Post by aGerman » 10 Mar 2011 16:30

Try something like that:

Code: Select all

@echo off &setLocal enableDelayedExpansion
set /p "old=old string ? "
set /p "new=new string ? "

for %%a in (r1.bat r2.bat r3.bat r4.bat) do (
  >new%%a type nul
  for /f "tokens=* delims=" %%b in (%%a) do (
    set "str=%%b"
    >>new%%a echo !str:%old%=%new%!
  )
)


Regards
aGerman

dukdpk
Posts: 13
Joined: 10 Mar 2011 03:27

Re: Replace text in bat files

#3 Post by dukdpk » 10 Mar 2011 22:54

Hi aGerman

I tried your code. It works. Many thanks

Post Reply