Sub-string expansion works within the set of a for
loop (that is the parenthesised part after in
) when immediate expansion is used (write %%I
instead of %I
in a batch file):
set "X=123"
for %I in (%X:~1,1%) do @echo %I
However, it fails when delayed expansion is applied:
for %I in (!X:~1,1!) do @echo %I
I would expect the output to be:
2
But instead it is:
!X:~1 1!
Why is this and how can I prevent that?
I know I could work around that by quoting the set and using ~
, but this is not what I want here:
for %I in ("!X:~1,1!") do @echo %~I
The following command line fails too:
for %I in (!X:*2=!) do @echo %I
The unexpected output is:
!
Also for
command lines using the /R
, /L
and /R
switches fail with such sub-string syntax.
It surely has got something to do with the fact that ,
and =
are token separators for cmd
, just like SPACE, TAB, ;
, etc.