.BAT exit several nested loops after the end of the corresponding list

I know that getting out of a nested loop is pretty simple, however I'm not sure how to do this when I work with multiple server lists. Here's the script:

Purpose: to search for sessions on the server that match a specific user ID, as well as to destroy all disconnected sessions found

Problem: I have several farm lists. I want to cycle through the lists until I find a user session, and then stop when this list is completed (it does not stop when the session is cleared, they may have several sessions in the farm).

Farmlist1.txt farmlist2.txt farmlist3.txt

If the session is found in the farmlist2.txt file, I want to end the search in this list, but do not continue the farmlist3.txt file.

That's what I still have, and it works like a charm. (Optimization is welcome)

@echo off
echoCitrix Session Reset
echo.
echo This will look for a specific userID AND kill disconnected sessions on     all servers!
set /p userid=User ID of the user: 
for %%a in (q:\scripts\1common\citrixlists\*.txt) do (
for /f "tokens=*" %%l in (%%a) do (
ping %%l -n 1 | find /i "TTL=" > nul 
if errorlevel 1 (
echo server %%l down or out of Load
) else (
echo Looking for %username% and killing disconnected sessions on %%l

for /f "tokens=3" %%b in ('qwinsta *tcp /server:%%l ^| find /i "%userid%"') do echo %%b | rwinsta %%b /server:%%l && echo SESSION FOR %userid% KILLED ON %%l

for /f "tokens=2" %%i IN ('qwinsta /server:%%l ^| find /i "disc"') DO (
if %%i gtr 0 (
rwinsta %%i /server:%%l && echo Disconnected sessions terminated
)

)

)

)

)   
0
source share
1 answer

I don’t quite understand what you are trying to accomplish, but I understand how to break out of nested loops for(or any other blocks of code nested in brackets). So let me dwell on this in more detail.

Breaking one loop foris simple: put goto :STOPin the loop and label :STOPon the line following the loop structure. The example below breaks a cycle depending on its cycle 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 for counting to 5, the output is:

0
1
2
3
Break!

, , . , 5 , 1000000.


( ):

  • :

    @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
    

    :

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

    , .

  • , . 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
    

    :

    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
    

    , .

  • script , . , , (call) , , goto:

    @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
    

    :

    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
    

    , .


, ( ) script.

0

Source: https://habr.com/ru/post/1651539/


All Articles