Benoit
Benoit

Reputation: 79233

How to perform substring substitution when the substring contains the equal sign?

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.

Upvotes: 4

Views: 704

Answers (1)

ghostdog74
ghostdog74

Reputation: 343057

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

Upvotes: 1

Related Questions