I would like to use a wildcard with the SET command in a Windows package, so I don't need to know exactly what is on the line to match it

I would like to use a wildcard with the SET command in a Windows package, so I don’t need to know exactly what is on the line to match it.

Is it possible?

If this has already been asked and answered, I'm sorry, I searched for a long time, but could not find it.

+2
source share
2 answers

A: Yes. But this is not as strong as it should be.

But first, let me answer a question that you did not ask (yet), because this is a natural follow-up question.

Q: Will the question mark match any single character in the search for a batch string and replace SET?
A: No. This is an ordinary character and will correspond only to him.

The asterisk IS is a wildcard and will match multiple characters, but will ONLY match all from the very beginning of the line . Not in the middle, not at the end.

Useful searches:

*x *how are you? 

The above two queries can be matched. The first will correspond to all, up to the first "x" with which he encounters. The second will correspond to all, up to the first "how are you?" He finds.

Legal, but not used, search:

 x* Hello* One*Three 

The above three search queries can NEVER be matched. Oddly enough, they are also legal and do not cause errors.
One exception: Hello * and x * WILL match themselves, but only if they are the very beginning of the line. (Thanks to Jeb!)

Two examples that you can enter or paste on the command line:

REM Successful search and replacement.
SET X = Hello my friend. How are you? SET X =% X: *. =%
ECHO Output: "% X%"

Conclusion: "How are you?"

REM An unexpected action that caused an unsuccessful search and replace.
SET X = Hello my friend. How are you? SET X =% X :. * =%
ECHO Output: "% X%"

Result: "Hello my friend. How are you?"

Logically,. * must match the time period, as a result of which the line is truncated to "Hello my friend." But since * only matches from the very beginning of the line ,. * nothing matches, and therefore the line remains unchanged.

+3
source

Exceptionally old but useful

As executable file set-ast

 set string=abc*def*ghi set-ast string "-dash-" :: -> "abc-dash-def-dash-ghi" 

If the arguments are specified as set-ast <variable-name> <replacement-string>


replace * with -dash- (only once)

Note: Slow expansion must be enabled.

 set string=abc*def :: -> "abc*def" set right=%string:**=% :: -> "def" set left=!string:%right%=! :: -> "abc*" set left=%left:~0,-1% :: -> "abc" set new-string=%left%-dash-%right% :: -> "abc-dash-def" 
0
source

All Articles