Code: Select all
if !ThisIsLegalNumber! EQU 1 if not defined break (
Perhaps if you show us your complete code (starting at the outer loop) I can write the exact modification you need...
Moderator: DosItHelp
Code: Select all
if !ThisIsLegalNumber! EQU 1 if not defined break (
dbenham wrote: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.
Ed Dyreen wrote:Squashman wrote:Code: Select all
:main ()
:: (
Code: Select all
:§processStart_ ( #sArgs,# #fullPathFile, #title,# #args,# #sErr,0 #imin,0, #imax,0, #coopLock,0 #clearEnv,0 )
for /L %%z in (1,1,10) do (
for %%a in (*.*) do (
if "%%~xa"==".txt" (
rem execute inner loop
echo %%a
)
)
echo %%z
)
foxidrive wrote:Why don't you put a condition in the inner loop and only execute the inner loop if the condition is true.
doscode wrote:I don't understand your code, why you write it this way.
if !ThisIsLegalNumber! EQU 1 %and% not defined break
What does it mean? I think that it doesn't solve anything. Will this break the loop or not? Next code there.
Code: Select all
rem Outer loop:
for %%D in (for-set) do (
some commands...
rem Inner loop:
for %%A in (for-set) do (
commands of inner loop...
)
more commands...
)
Code: Select all
rem Outer loop:
for %%D in (for-set) do (
some commands...
rem Inner loop:
SET BREAK=
for %%A in (for-set) do (
IF NOT DEFINED BREAK (
commands of inner loop...
IF SOME_CONDITION SET BREAK=TRUE
) <- closing parentheses of new IF
) <- original closing parentheses of inner FOR
more commands...
)
Code: Select all
rem Outer loop:
for %%D in (for-set) do (
some commands...
rem Inner loop:
for %%A in (for-set) do ( <- there are NOT commands
if condition ( <- ... between previous FOR and this IF
commands of inner loop...
) <- there are NOT commands
) <- ... between closing IF and closing FOR
more commands...
)
Code: Select all
rem Outer loop:
for %%D in (for-set) do (
some commands...
rem Inner loop:
SET BREAK=
for %%A in (for-set) do ( <- there are NOT commands
if condition IF NOT DEFINED BREAK ( <- ... between previous FOR and this IF
commands of inner loop...
IF SOME_CONDITION SET BREAK=TRUE
) <- there are NOT commands
) <- ... between closing IF and closing FOR
more commands...
)
Code: Select all
if condition if not defined break (
Code: Select all
if condition %and% not defined break (
Code: Select all
rem Outer loop:
for %%D in (for-set) do (
some commands...
rem Inner loop:
SET BREAK=
for %%A in (for-set) do IF NOT DEFINED BREAK (
commands of inner loop...
IF SOME_CONDITION SET BREAK=TRUE
) <- original closing parentheses of inner FOR
more commands...
)
dbenham wrote:What hasn't been explained is that the variable from the outer loop is available within the inner loop
Dave Benham
Code: Select all
Remember, FOR variables are single-letter, case sensitive, global,
and you can't have more than 52 total active at any one time.
Code: Select all
@echo off
for %%A in (GlobalA-1 GlobalA-2) do (
for %%B in (B-1 B-2) do (
echo/
echo %%A %%B
for %%A in (LocalA-1 LocalA-2) do (
echo %%A %%B
)
)
)
Code: Select all
GlobalA-1 B-1
LocalA-1 B-1
LocalA-2 B-1
GlobalA-1 B-2
LocalA-1 B-2
LocalA-2 B-2
GlobalA-2 B-1
LocalA-1 B-1
LocalA-2 B-1
GlobalA-2 B-2
LocalA-1 B-2
LocalA-2 B-2
Aacini wrote:You may get an equivalent behaviour of breaking the inner loop and passing to the next iteration of the outer loop if you enclose the body of inner loop in an IF controled by a break variable:Code: Select all
@echo off
for %%D in (1 2 3) do (
set break=
for %%A in (1 2 3) do (
if not defined break (
echo D=%%D, A=%%A
if %%A equ 2 set break=TRUE
)
)
)
Antonio
doscode wrote:I think I have two options. Either to use inner condition (cycles would not be skipped) or to move the inner code to separated subroutine and to call this subroutine from the main loop. Then I can use exit /b
Aacini wrote:You may also use this approach, that is simpler to write:Code: Select all
rem Outer loop:
for %%D in (for-set) do (
some commands...
rem Inner loop:
SET BREAK=
for %%A in (for-set) do IF NOT DEFINED BREAK (
commands of inner loop...
IF SOME_CONDITION SET BREAK=TRUE
) <- original closing parentheses of inner FOR
more commands...
)
Code: Select all
for ... () do (
if !cnt! EQU 0 SET BREAK=
REM inner loop:
for ... () do (
<-- some code here -->
if !ThisIsLegalNumber! EQU 1 (
SET legalNumber=!second!
SET /A cnt=!cnt!+1
if !cnt! LSS 4 (
SET IP=!IP!!legalNumber!.
) else (
SET IP=!IP!!legalNumber!
[b]echo IP complete:!IP![/b]
SET /A cnt=0
SET /A Search_IP=0
SET /A breaked=1
SET BREAK=TRUE
echo Script goes on ...
)
)
)
) <-------- Endof inner loop
<-- go here - next code here -->
)
Aacini wrote:dbenham wrote:What hasn't been explained is that the variable from the outer loop is available within the inner loop
Dave Benham
This point is "explained" in the FOR help this way:Code: Select all
Remember, FOR variables are single-letter, case sensitive, global,
and you can't have more than 52 total active at any one time.
Didn't you see it? It is the global word! This mean that a FOR variable is the same in any ACTIVE FOR at any nesting level, don't matter if the nested FOR is placed in the same nesting code or in a called subroutine!
Aacini wrote:You may get an equivalent behaviour of breaking the inner loop and passing to the next iteration of the outer loop if you enclose the body of inner loop in an IF controled by a break variable:Code: Select all
@echo off
for %%D in (1 2 3) do (
set break=
for %%A in (1 2 3) do (
if not defined break (
echo D=%%D, A=%%A
if %%A equ 2 set break=TRUE)))
Code: Select all
@echo off
set "key=HKCU\certain\Registry\Key"
setlocal EnableDelayedExpansion
for %%D in (1 2 3) do (
set "count=1" & set break=
for /l %%A in (0,1,!count!) do (
if not defined break (
reg query %key%%%A >nul 2>nul
if errorlevel 1 (set break=TRUE
) else (echo %key%%%A & set /a "count+=1"))))
exit /b