1

Execute the following 3 statements and see the result.

set myvar=11?22*33

echo %myvar:?=_%

echo %myvar:*=_%

On Win7/10, The second one replaces a question mark with an underscore, great.

But the third one fails with =_% was unexpected at this time. So, I'd like to ask:

  • What is the special meaning of * here?
  • How to have batch statement replace * with underscore?

enter image description here

==== Update ====

Well, this has been asked by someone and got answers: I need to match or replace an asterisk * in a batch environmental variable using only native Windows commands. Is this possible?

Jimm Chen
  • 3,411
  • 3
  • 35
  • 59
  • The asterisk is a special character, a wildcard/glob, in your expansion command, and is not therefore seen as a literal character. Batch files are not designed for replacing characters in strings, and you should therefore use one of the other built in true scripting languages, _(for instance, VBScript / JScript / PowerShell)_. – Compo Mar 15 '22 at 17:15
  • 2
    Please open a [command prompt](https://www.howtogeek.com/235101/), run `set /?` and read the output usage help. There is described: __"str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1.__ So the asterisk `*` has a special meaning on string substitutions on being found after `VariableName:` by `cmd.exe` – Mofi Mar 15 '22 at 17:44
  • To replace the asterisk, you need to loop through the characters of the string, using [sub-string expansion](https://ss64.com/nt/syntax-substring.html)… – aschipfl Mar 15 '22 at 19:04

0 Answers0