Cannot exit from For loop to another for loop

Using the script package, I will contact to break out of the inner for loop and land on the outer loop to continue the sequence, but it returns an error:

The syntax of the command is incorrect

This refers to the shortcut : breakerpoint . Please advise. Thank.

for /l %%A in (1, 1, %NumRuns%) do (
echo Doing run %%A of %NumRuns%

    for /l %%B in (1, 1, 3) do (
        ping 127.0.0.1 -n 2 > NUL
        tasklist /FI "IMAGENAME eq Reel.exe" 2>NUL | find /I /N "Reel.exe">NUL
        echo innerloop top
    echo Error lvl is %ERRORLEVEL%
    if NOT "%ERRORLEVEL%"=="0" (
        echo innerloop middle
        goto:breakerpoint 
    )
    echo innerloop bottom
)
taskkill /F /IM "Reel.exe"
:breakerpoint rem this is error line

)
:end
echo end of run

pause
+4
source share
1 answer

A gotoalways interrupts the current code block, and the current code block is a complete block starting with the first bracket, in your case you leave everything nested at FORonce.

To avoid this, you need to use helper functions.

for /L %%A in (1, 1, %NumRuns%) do (
    echo Doing run %%A of %NumRuns%
    call :innerLoop
)
exit /b

:innerLoop
for /L %%B in (1, 1, 10) do (
  echo InnerLoop %%B from outerLoop %%A
  if %%B == 4 exit /b
)
exit /b
+3
source

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


All Articles