sambul35 wrote:Thanks, now it works OK. I wonder, why with some commands like ECHO the same delayed expended variable works normally, and with others like IF EXIST it doesn't without escapes?
The command line interpreter (cli) splits actual statement into tokens to detect which statement has to be executed:
This is where the ',' seperates the tokens and fails.
You may use doublequotes instead of '^' to prevent the filename to be split into parts on delimiters:
Code: Select all
if not exist "!dsk:~0,3!nul" echo no
It may be more intuitive.
The echo command is split into tokens, too.
But if no special case is detected then tokenization of the argument string is discarded and the complete argument string is interpreted as the print_string token:
Code: Select all
@echo off
:: print actual echo state: statement ::= keyword("echo")
echo
:: print echo help screen: statement ::= keyword("echo") token("/?") token*
echo ^ =;, /? abcdefg
:: switch echo state to on: statement ::= keyword("echo") token("ON")
echo ON
:: switch echo state to off: statement ::= keyword("echo") token("OFF")
echo OFF
:: no other case: statement ::= keyword("echo") token(print_string("ON OFF /?"))
echo ON OFF /?
:: prove that echo is a token, and the right is handled as one token (first comma disappears):
echo,a,b
::force the string to be interpreted as a print_string: well THAT is hard to explain;
:: it seems that compount statements have a higher priority to be recognized although there is no compount statement present here...
:: but could also be caused by other side effects - so this is just a guess
echo(/?
:: similar (guessed) explaination for these:
echo\/?
echo//?
echo./?
:: ...
goto :eof
sambul35 wrote:At 1st look IF EXIST doesn't require tokenization in contrast with FOR loop...
Why should the if exist doesn not need tokenization?
If you use the statement "if not exist filename statement" then "if", "exist", filename, and statement are all tokens (from the parsers point of view).
penpen