MSDOS Batch File String Substitution Using Variables

jamesnoe
jamesnoe used Ask the Experts™
on
Hi,

I want to perform some string manipulation

Say I have the following:

set myString=Hello How Are You
set replaceThis=Hello
set withThis=James

I really want to do:

set output=%myString:%replaceThis%=%withThis%%

So far, the closest I've got is:

setlocal EnableDelayedExpansion
set output=%myString:Hello=!withThis!%

But I cannot work out how to replace Hello with %replaceThis

Any help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Test your restores, not your backups...
Top Expert 2016
Commented:
Try this:

setlocal EnableDelayedExpansion
set myString=Hello How Are You
set replaceThis=Hello
set withThis=James
set output=!myString:%replaceThis%=%withThis%!
echo %output%

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial