An example:
FOR %%i IN (3) DO (
ECHO %%i
FOR /R "C:\backup\server%%i\temp" %%? IN (*.bak) DO (
REM some code...
)
)
Result of ECHO
: 3
.
Result of second FOR
: the code isn't executed.
But with:
FOR %%i IN (3) DO (
ECHO %%i
FOR /R "C:\backup\server3\temp" %%? IN (*.bak) DO (
REM some code...
)
)
Result of ECHO
: 3
.
Result of second FOR
: the code is executed.
Any idea?
The reason the original code does not work is because the directory for the FOR /R option must be known at the time the FOR is parsed, but the %%i variable is not expanded until after the FOR statement has already been parsed. The same problem exists for the adarshr suggestion - !suffix! is not expanded until after the FOR statement has been parsed.
The GG answer is a very good viable work-around. Perhaps could be improved with PUSHD followed by POPD at end, instead of CD. But only if needed.
The only other way I can think to get around the problem is something like this
@echo off
FOR %%i IN (3) DO call :innerLoop %%i
exit /b
:innerLoop
FOR /R "C:\backup\server%1\temp" %%? IN (*.bak) DO (
REM some code...
)
exit /b
But I don't like to use CALL unless I have to because it is inefficient. I like the CD solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With