Replacing a string inside a block

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Replacing a string inside a block

#1 Post by sambasiva » 02 Jun 2013 10:36

Hi Experts,

I have a requirement to replace a search string ( which has been set inside a block ) with some replacement string.
Below is the code I have. Which is not working.

@echo off
setlocal enabledelayedexpansion
set SAMPLE=sample
set REPLACETEXT=hello

if %SAMPLE% == sample (
SET string=how abt bath
set SEARCHTEXT=bath
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified!
)

I am not sure about the exact syntax to nest the variables inside a block.

Could someone help me with this script ?

Thanks in Advance
Sambasiva

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Replacing a string inside a block

#2 Post by Aacini » 02 Jun 2013 11:35

Code: Select all

@echo off
setlocal enabledelayedexpansion
set SAMPLE=sample
set REPLACETEXT=hello

if %SAMPLE% == sample (
SET string=how abt bath
set SEARCHTEXT=bath
for /F "tokens=1,2" %%a in ("!SEARCHTEXT! !REPLACETEXT!") do SET modified=!string:%%a=%%b!
echo !modified!
)

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: Replacing a string inside a block

#3 Post by sambasiva » 02 Jun 2013 12:02

Excellent...It worked..
Thanks a lottttttttttttttttttt....

Post Reply