.BAT break out of multiple nested loop, after finishing the respective list

▼魔方 西西 提交于 2019-11-28 14:45:52

I do not exactly understand what you are trying to accomplish, but I do understand how to break out of nested for loops (or any other nested parenthesised blocks of code). So let me elaborate on that.

Breaking a single for loop is simple: put goto :STOP into the loop and the label :STOP in the line following the loop structure. The example below breaks the loop depending on its loop counter value:

@echo off
for /L %%I in (0,1,5) do (
    if %%I GTR 3 (
        echo Break!
        goto :STOP
    )
    echo %%I
)
:STOP

Although the loop is specified to count up to 5, the output is:

0
1
2
3
Break!

Note that the loop actually completes the counting internally, but no more commands are executed. You can easily reproduce that when you increase the end value 5 to a huge number like 1000000.


But now let us concentrate on nested loops (two in each example):

  1. The following code snippet breaks both loops upon a certain condition is met:

    @echo off
    for %%J in (Aa Bb Cc) do (
        for /L %%I in (0,1,5) do (
            if "%%~J"=="Bb" if %%I GTR 3 (
                echo Break!
                goto :STOP
            )
            echo %%J-%%I
        )
    )
    :STOP
    

    The output is:

    Aa-0
    Aa-1
    Aa-2
    Aa-3
    Aa-4
    Aa-5
    Bb-0
    Bb-1
    Bb-2
    Bb-3
    Break!
    

    As you can see, execution of the loop construct is interrupted immediately at a certain point.

  2. The batch file here breaks the outer loop when the a certain condition in the inner loop is met. The result of the check is transferred to the outer loop using a variable FLAG:

    @echo off
    set "FLAG="
    for %%J in (Aa Bb Cc) do (
        for /L %%I in (0,1,5) do (
            if "%%~J"=="Bb" if %%I GTR 3 (
                echo Break!
                set "FLAG=#"
            )
            echo %%~J-%%I
        )
        if defined FLAG goto :STOP
    )
    :STOP
    

    The output is:

    Aa-0
    Aa-1
    Aa-2
    Aa-3
    Aa-4
    Aa-5
    Bb-0
    Bb-1
    Bb-2
    Bb-3
    Break!
    Bb-4
    Break!
    Bb-5
    

    You will notice that the inner loop finishes execution before the outer one is broken.

  3. The script below breaks the inner loop when a certain condition is net. To not break the outer loop, a sub-routine holding the inner loop and being called (call) from the outer one is required to hide the code block context of the outer loop from the goto break-up method:

    @echo off
    for %%J in (Aa Bb Cc) do (
        call :SUB "%%~J"
    )
    goto :EOF
    
    :SUB
    for /L %%I in (0,1,5) do (
        if "%~1"=="Bb" if %%I GTR 3 (
            echo Break!
            goto :STOP
        )
        echo %~1-%%I
    )
    :STOP
    

    The output is:

    Aa-0
    Aa-1
    Aa-2
    Aa-3
    Aa-4
    Aa-5
    Bb-0
    Bb-1
    Bb-2
    Bb-3
    Break!
    Cc-0
    Cc-1
    Cc-2
    Cc-3
    Cc-4
    Cc-5
    

    Here the outer loop finishes its execution without being affected by the broken inner one.


I hope one of the (last two) examples suits the requirements for your script.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!