How can I echo the string %I
in a secure way, independent on whether or not the echo
command line is placed within a for %I
loop?
For example, in command prompt (cmd
):
>>> rem // `echo` is outside of `for` scope, so this works:
>>> echo %I
%I
>>> rem // `echo` is inside of `for` scope, so `%I` is resolved:
>>> for %I in (.) do @echo %I
.
>>> rem // escaping does not help, `%I` is still resolved:
>>> for %I in (.) do @(echo ^%I & echo %^I & echo ^%^I & echo %%I)
.
.
.
%.
And in a batch file...:
@echo off & rem // Here you need to double the `%` signs!
rem // `echo` is outside of `for` scope, so this works:
echo %%I & echo %%%%I
echo/
rem // `echo` is inside of `for` scope, so `%%I` is resolved:
for %%I in (.) do (echo %%I & echo %%%%I)
echo/
rem // escaping does not help, `%%I` is still resolved:
for %%I in (.) do (echo ^%%I & echo %%^I & echo ^%%^I & echo ^%^%I & echo ^%^%^I & echo ^%%^%%^I)
...the result is:
%I %%I . %. . . . I ^I %.
So how do I have to change the above approaches (both cmd
and batch file) to get %I
echoed?
There are two ways to work around the unintended expansion of the
for
reference%I
:Delayed Expansion
In
cmd
:In a batch file...:
...with the result:
However, delayed expansion could harm under certain circumstances; so could the environment localisation done by
setlocal
/endlocal
.But there is still another way:
Wrap Around Another
for
LoopIn
cmd
:In a batch-file...:
...with the result:
Although you cannot echo
%%J
now any more, of course.Wrap Around Another
for
Loop (edited according to dbenham's suggestion)In
cmd
:In a batch-file...:
...with the result:
This time even
%%J
could be echoed.