Logically you might want something like the following: (pseudo code)
Code: Select all
for var1 in (list1) do (
for var2 in (list2) do (
do something with var1 and var2
if var2=EndCondition break out of loop2
)
)
As has been pointed out, you need to use a called subroutine for the inner loop, and you can safely break out of the inner loop and return to the outer loop using GOTO.
But you might fret, the context of the outer loop dissapears while I am in the called subroutine, so I can't access the outer loop variable. No worries...
What hasn't been explained is that the variable from the outer loop is available within the inner loop
Given that we are in a subroutine, a simpler approach to breaking out of the inner loop is to use EXIT /B instead of GOTO :LABEL.
Here is an example that demonstrates both concepts. Note how %%A is initially not available within the subroutine. But while inside the inner loop it "magically" reappears.
Code: Select all
@echo off
for %%A in (1 2 3) do (
echo(
echo inside outer loop: A=%%A
call :innerSub
)
exit /b
:innerSub
echo inside innerSub but before inner loop: A=%%A
for %%B in (a b c e e) do (
echo inside inner loop: A=%%A B=%%B
if %%B==c exit /b
)
exit /b
output:
Code: Select all
inside outer loop: A=1
inside innerSub but before inner loop: A=%A
inside inner loop: A=1 B=a
inside inner loop: A=1 B=b
inside inner loop: A=1 B=c
inside outer loop: A=2
inside innerSub but before inner loop: A=%A
inside inner loop: A=2 B=a
inside inner loop: A=2 B=b
inside inner loop: A=2 B=c
inside outer loop: A=3
inside innerSub but before inner loop: A=%A
inside inner loop: A=3 B=a
inside inner loop: A=3 B=b
inside inner loop: A=3 B=c
Dave Benham