我怎样才能打破"出于For循环?

时间:2018-05-01 17:27:25

标签: windows batch-file cmd

下面,当我执行goto命令时,它只是挂起而且我必须使用Control-C。我也试过了EXIT /b。我尽可能地避免转到goto。有办法做我想要的吗?

:SUB_bigRandLooper

set /a lcv=0

FOR /L %%s IN ( 0 , 0 , 1 ) DO (

    set big-rand=!random:~-4!
    echo big-rand is !big-rand!
    set /a lcv=%lcv+1
    if !big-rand! GTR 9900 goto bigRandLooperWrapup
)

:bigRandLooperWrapup

echo biggest-rand is %big-rand%
echo lcv is %lcv%

EXIT /B

1 个答案:

答案 0 :(得分:2)

简短的回答是:不,你不能。

由于您正在使用N并建立一个无限循环,并且循环在执行之前已经过预处理和缓存,因此for /L不会中断它; goto打破了循环上下文,或更正确地说出goto / (块上下文,因此块中不再执行任何命令,但循环本身仍在运行。

您可以通过以下代码证明这一点:

)

您会看到for /L %%I in (1,1,100000) do ( echo %%I if %%I equ 10 goto :SKIP ) :SKIP echo skipped 仅从echo %%I %%I执行1,但执行不会立即在10继续执行,但有一个echo skipped值得注意的延迟,因为循环在后台完成迭代,但不再执行任何命令。

虽然有一种解决办法:你可以用goto建立无限循环,如下所示:

:SUB_bigRandLooper

set /A lcv=0
:bigRangLooperLoop
set big-rand=!random:~-4!
echo big-rand is !big-rand!
set /A lcv+=1
if !big-rand! gtr 9900 goto :bigRandLooperWrapup
goto :bigRangLooperLoop

:bigRandLooperWrapup
echo biggest-rand is %big-rand%
echo lcv is %lcv%
exit /B

我知道goto循环比for /L循环慢,但这是创建可破坏无限循环的唯一方法。

更快的方法是嵌套两个循环方法:使用for /L迭代几千次并包裹无限goto循环。

另一种解决方法是利用exit命令可以中断(无限)for /L循环这一事实。但由于这也会退出批处理文件正在运行的cmd实例,因此需要将循环放入单独的cmd实例中。当然,环境与当前环境完全分开。解决方案可能如下所示:

:SUB_bigRandLooper
@echo off
rem // Check for argument, interpret it as jump label if given:
if not "%~1"=="" goto %~1

rem /* Establish explicit `cmd` instance for executing the `for /L` loop;
rem    the `for /F` loop implicitly creates the new `cmd` instance for the command
rem    it executes, so we do not have to explicitly call `cmd /C` here; the resulting
rem    values are echoed by the sub-routine and captured here by `for /F`: */
for /F "tokens=1,2" %%I in ('"%~f0" :bigRandLooperLoop') do (
    rem // Assign captured values to environment variables:
    set "big-rand=%%I" & set "lcv=%%J"
)

:bigRandLooperWrapup
echo biggest-rand is %big-rand%
echo lcv is %lcv%
exit /B

:bigRandLooperLoop
setlocal EnableDelayedExpansion
set /A lcv=0
for /L %%s in (0,0,1) do (
    set big-rand=!random:~-4!
    rem /* Explicitly redirect this output to the console window to prevent it from
    rem    being captured by `for /F` in the main routine too: */
    > con echo big-rand is !big-rand!
    set /A lcv+=1
    if !big-rand! gtr 9900 (
        rem // Output found values in order to be able to capture them by `for /F`:
        echo !big-rand! !lcv!
        rem // Break loop and leave current `cmd` instance:
        exit
    )
)
endlocal