1

is here a command to exit from a FOR without using a "goto label"?

for %%c in (list) do (
 [some code]
 for %%i in (1 1 100) do (
   [some code]
   if [a condition] exit-this-for
   [some code]
   )
rem go here when exit
 [some code]
 )

i hate the labels!

2
  • I don't know what shell is this but most languages have the 'break' keyword to escape from loops Dec 12, 2015 at 12:00
  • @TorbalanBG In batch (Windows/DOS) break is for turning on/off the ctrl+c sequence.
    – jscott
    Dec 12, 2015 at 13:11

1 Answer 1

1

Even if you hate labels, refusing to use them limits your options here. I can't think of an easy way to do this within a single file. If you don't hate the idea of using two batch files, you can do something like this. Running a.cmd will CALL b.cmd and perform ERRORLEVEL check based on any EXIT /B in b.cmd.

a.cmd:

REM a.cmd  
@ECHO OFF
SET CHECKVAR=10

CALL b.cmd

IF ERRORLEVEL %CHECKVAR% (
    REM go here when exit from b.cmd met condition
    ECHO "B was %CHECKVAR%"
) ELSE (
    REM go here when exit from b.cmd did not meet condition
    ECHO "B was not %CHECKVAR%"
)

b.cmd

REM b.cmd
@ECHO OFF

FOR /L %%A IN (0,1,3) DO (
    ECHO "A: %%A"
    FOR /L %%B IN (0,1,100) DO (
        ECHO "B: %%B"
        REM Do what ever conditional check you like here.
        REM Just need to return some ERRORLEVEL to a.cmd.
        IF %%B==%CHECKVAR% (
            EXIT /B %CHECKVAR%
        )
    )
)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .