0

This Batch file code snippet

@ECHO OFF
FOR %%i IN (1 2 3) DO (
    ECHO %%i "%%i" %%a "%%a"
    FOR /F "skip=%%i" %%j IN ("abc"
                              "abc"
                              "abc") DO ( ECHO. )
)

produces

1 "1" %a "%a"
%i" was unexpected at this time.
2 "2" %a "%a"
%i" was unexpected at this time.
3 "3" %a "%a"
%i" was unexpected at this time.

%%i is expanded properly (to 1,2,3) on the third line. However, on the fourth line %%i seems to expand to just %i. Apparently, the interpreter does not recognize on the forth line %%i as something that needs to be expanded and rather treats %% as the usual escape sequence for % and i just as i. Why does it happen? Is it just a bug (the interpreter is just implemented this way) or there is some logic in this behavior? if so, is it mentioned somewhere in the documentation?

zesy
  • 481
  • 5
  • 12
  • 2
    The interpreter is just implemented this way. The command-modifiers in a `for/f` (skip, tokens, etc.) and the start-direcrory in a `for/r` must be able to be resolved at parse-time of the outer loop. Try passing the values to a subroutine. – Magoo Nov 08 '22 at 18:30
  • 1
    [This answer](https://stackoverflow.com/a/39653402) should explain why this cannot work… – aschipfl Nov 08 '22 at 22:37

0 Answers0