tags:

views:

40

answers:

1

Hello!

In Windows Batch files, you can use this syntax to perform search&replace on variables:

set myvar=%myvar:foo=bar%

How to do this, however, when “foo” or “bar” include an equal sign? Escaping it with ^ does not seem to work…

Thank you.

+1  A: 

Instead of battling with cmd.exe's quirks, why not use vbscript instead?

Set objArgs = WScript.Arguments
strOld=objArgs(0)
strNew=objArgs(1)
str=Replace(WScript.StdIn.ReadAll,vbCrLf,"")
WScript.Echo Replace(str,strOld,strNew)

Save the above as replace.vbs

Usage:

C:\test>echo test| cscript //nologo replace.vbs te mi
mist
ghostdog74
Or JScript or PowerShell or Perl or .... Lots of questions do deserve the "Don't do that, use this instead" type answers, I don't think this is one of them.
Logan Capaldo
So ? If OP has no restrictions on what tools to use, he is free to adopt whatever solution he desires. Besides, I am only speaking the truth about cmd.exe's limitation and quirkiness.
ghostdog74
Actually I do have restrictions on what my script may do because they will be deployed in a production environment on customer servers. I agree this would be really comfortable though.
Benoit
Windows machines by default comes installed with vbscript.
ghostdog74