1.

Solve : Replace String Using SET Command?

Answer»

Hello,
I need to replace a STRING in a variable with another string. The second or replacing string is also in a variable.
I USE the SET command to do the string replacement and I can't figure out how to replace a string with a variable.

For example:
set a=Hello kitty
set b=doggy

To change "Hello kitty" to "Hello doggy" with a literal:
set a=%a:kitty=doggy%
Result is: Hello doggy

To replace the string with a variable, I tried the following and failed:
set a=%a:kitty=%b%
The result is: Hello b

The % in front of the variable b is causing the problem. Can anyone give me some suggestions on fixing my problem?

Thank you.set a=%a:Kitty=%%b%%Thank you for your reply.

That seemed to work but when I tried to manipulate the first string like echo or concatenate, the word doggy is repeated again.

set a=%a:kitty=%%b%%

Echo %a%
set a=%a%again
Echo %a%

Result is:
Hello doggy (this CAME from the first set command)
Hello doggydoggy (this came from the first Echo command)
Hello doggydoggyagain (this came from the second Echo command)

Any suggestions?Oops. Your suggestion works. I had too many set commands in my script so it appeared that the word doggy was repeated. Sorry.

Thank you very much for your help.That's GOOD!



Discussion

No Comment Found