无法突破For循环到另一个For循环

时间:2016-08-17 12:42:36

标签: batch-file

使用批处理脚本,我想要打破内部for循环并落在外部for循环上继续执行序列但它返回错误说:

The syntax of the command is incorrect

指的是我的:破坏点标签。请指教。谢谢。

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

1 个答案:

答案 0 :(得分:3)

goto总是打破当前代码块,当前代码块是从第一个括号开始的完整块,在您保留所有嵌套FOR的情况下。

为避免这种情况,您需要使用子功能。

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