I know breaking out of a nested loop is fairly easy, however, I'm not sure how to do it when I'm working with multiple lists of servers. Here's the scenario:
Goal: Search for sessions on a server matching a specific user ID, and also kill any disconnected sessions found
Problem: I have multiple lists of farms. I want to cycle through the lists until I find the users session, and then stop when that list is finished (not stop when the session is cleared, they may have multiple sessions in the farm).
Farmlist1.txt farmlist2.txt farmlist3.txt
If the session is found in farmlist2.txt, I want to FINISH searching in that list, but don't continue to farmlist3.txt.
Here's what I have so far, and it works like a charm. (Optimizations welcomed)
@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
)
)
)
)
)
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: putgoto :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:Although the loop is specified to count up to
5
, the output is: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 like1000000
.But now let us concentrate on nested loops (two in each example):
The following code snippet breaks both loops upon a certain condition is met:
The output is:
As you can see, execution of the loop construct is interrupted immediately at a certain point.
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
:The output is:
You will notice that the inner loop finishes execution before the outer one is broken.
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 thegoto
break-up method:The output is:
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.