For/if parsing under delayedExpansion vs. general rules
Posted: 02 Mar 2014 23:27
For/if commands seem to fail when executed via a delayed expansion, or a 'call'.
Output:
It's not about internal commands, since 'echo' still works in all cases, but it appears to be a parsing anomaly with for/if specifically. And the 'call' failure in particular doesn't seem to follow from the parsing rules at http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts. According to those, the 1st iteration would proceed to phase 6, then loop back and rerun phases 1 and 2, where the 'if' and 'for' tokens should be detected in phase 2 of the 2nd pass. Maybe I am missing or misreading something.
Liviu
Code: Select all
@echo off & setlocal disableDelayedExpansion
echo(
set "echo.ok=echo 123"
set "if.fail.edx=if 1==1 (echo 1==1) else (echo ???)"
set "for.fail.edx=for %%X in (x y) do echo '%%X'
@rem all work ok
%echo.ok%
%if.fail.edx%
%for.fail.edx%
setlocal enableDelayedExpansion
@rem only 'echo' works when delayed-expanded
echo(
!echo.ok!
!if.fail.edx!
!for.fail.edx!
echo(
for %%Z in (1) do !echo.ok!
for %%Z in (1) do !if.fail.edx!
for %%Z in (1) do !for.fail.edx!
echo(
call !echo.ok:%%=%%%%!
call !if.fail.edx:%%=%%%%!
call !for.fail.edx:%%=%%%%!
Code: Select all
123
1==1
'x'
'y'
123
'if' is not recognized as an internal or external command,
operable program or batch file.
'for' is not recognized as an internal or external command,
operable program or batch file.
123
'if' is not recognized as an internal or external command,
operable program or batch file.
'for' is not recognized as an internal or external command,
operable program or batch file.
123
'if' is not recognized as an internal or external command,
operable program or batch file.
'for' is not recognized as an internal or external command,
operable program or batch file.
It's not about internal commands, since 'echo' still works in all cases, but it appears to be a parsing anomaly with for/if specifically. And the 'call' failure in particular doesn't seem to follow from the parsing rules at http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts. According to those, the 1st iteration would proceed to phase 6, then loop back and rerun phases 1 and 2, where the 'if' and 'for' tokens should be detected in phase 2 of the 2nd pass. Maybe I am missing or misreading something.
Liviu