Extending a substring with an empty string causes an error in the If condition

I have the following code snippet:

if "%ARGV:~,1%"==":" echo %ARGV% begins with a colon. 

While the ARGV variable contains a non-empty value or is correctly spoken, it is defined, everything works as expected, so if the line in ARGV starts with a colon, the echo command is executed.

However, as soon as I clear the ARGV variable, a syntax error occurs:

 echo was unexpected at this time. 

What's going on here? The syntax is perfectly correct, but why does this command line fail?

Even one of the most useful topics, How is the Windows Command Interpreter Script Interpreter (CMD.EXE)? because such things do not provide an explanation for this behavior.

When I do the same directly on the command line, everything is fine. Moreover, when I try to use a deferred extension , the error also does not occur.

+7
cmd batch-file syntax-error
source share
1 answer

My co-author answer to jeb answer to "How is the Windows command interpreter (CMD.EXE) parsing scripts?" explains the behavior.

My companion answer provides the necessary information about how the% extension works in order to fully predict the behavior.

If you save ECHO ON, you can see the result of the extension, and the error message makes sense:

test.bat

 @echo on @set "ARGV=" if "%ARGV:~,1%"==":" echo %ARGV% begins with a colon. 

- output -

 C:\test>test echo was unexpected at this time. C:\test>if "~,1" echo begins with a colon. 


The important rules of my answer explaining the result of the extension are:

1) (Percent) Starting from the left side, scan each character for % . If found, then

  • 1.1 (escape % ) ... not relevant
  • 1.2 (expand argument) ... not relevant
  • 1.3 (expand variable)
    • Else, if command extensions are disabled, then ... not relevant
    • Otherwise, if command extensions are enabled, then look at the next line of characters, breaking up to % : or <LF> and name them VAR (there may be an empty list). If the VAR breaks to : and the subsequent % character, then include : as the last character in the VAR and a break before % .
      • If the next character is % , then replace %VAR% with VAR (replace with nothing if no VAR is defined) and continue scanning
      • Otherwise, if the following character : then
        • If the VAR value is undefined, then remove %VAR: and continue scanning.
        • ... the rest does not matter

Beginning with

 if "%ARGV:~,1%"==":" echo %ARGV% begins with a colon. 

Extending a variable expands all of the following lines to zero, because the variable is undefined:

 %ARGV: %"==": %ARGV% 

And you are left with:

 if "~,1" echo begins with a colon. 

It works with expansion delay because the IF statement is parsed before slow expansion (explained in jeb's answer in phase 2)

Everything works from the command line because the extension of the command line variable does not delete the line when the variable is not defined. (poorly explained in the jeb answer at the bottom inside CmdLineParser :, Phase1 (Percent))

+5
source share

All Articles