It fails because the comma is a token delimiter that must be either quoted or escaped if used within a delayed expansion expression as part of an IF statement. The full explanation requires a full understanding of the batch parser. But in short, the IF command is one of 2 commands that get a special parsing pass that is sensitive to token delimiters (space, colon, semicolon, equal sign, 0xFF). They are not a problem when using normal percent expansion because that occurs before the IF special parsing. But the delayed expansion occurs after the IF special parsing, so the token delimiters must either be escaped or quoted.
The square brackets have no special meaning - they simply guarantee the expression is not empty, which is needed when you use normal expansion. You could use any single character at the beginning or end of each side. But with delayed expansion, the IF parser sees the expression before expansion, so it is guaranteed to be non-empty, and the extra characters are not needed.
Either of the following will work:
Code: Select all
if "!arg:~0,2!" equ "/t"
if !arg:~0^,2! equ /t
If you are looking to implement named parameters for a batch script, then you should have a look at
http://stackoverflow.com/a/8162578/1012053Dave Benham