Solved Nested for loops and Delayed Exapansion

January 28, 2016 at 12:58:26
Specs: Windows 10

I'm a beginner a batch scripting, so please forgive me if this is a stupid question.

In batch scripting is it possible to use for a for loop inside a for loop?

Could you explain, simply and with examples, how Delayed Expansion works - particularly with respect to the secondary for loop.


See More: Nested for loops and Delayed Exapansion

Report •

January 31, 2016 at 19:59:53
✔ Best Answer
Best I can do is give a simplified example. You can probably google for more accurate and robust coverage of batch. Much is provided simply by: FOR /?
SET /?
IF /?
from the cmd window. The following tries to demonstrate nested FOR, and calls to external routines which can get around the delayedexpansion:

@echo off & setlocal enabledelayedexpansion
for /l %%a in (1 1 5) do (
for /l %%b in (100 1 105) do (
:: set a "regular" variable to a "system" var value
set v=%%b
:: show result using both notations
echo outer: %%a inner: %%b *** b to v... v: perc: %v% exclm: !v!
:: call out to external subroutine with no arguments
call :aa
:: this is much easier
call :bb %%a %%b
goto :eof

echo now in subrutine aa, v is: perc: %v% exclm: !v!
for %%z in (1) do (
echo system var.s recovered in sub aa: ---a: %%a ---b: %%b
goto :eof

echo subrtn bb, input values: %1 ***** %2
goto :eof

Report •
Related Solutions

Ask Question