本文介绍了“继续" Windows Batch中嵌套循环中的等效命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,其中包含类似于continue的命令的嵌套循环:

I have a batch file which contains nested loop with continue-like command:

for %%i in (1, 2, 4, 8, 16, 32, 64, 128, 256) do (
    for %%j in (1, 2, 4, 8, 16, 32, 64, 128, 256) do (
        if %%i gtr %%j goto CONTINUE
        test.exe 0 %%i %%j 100000 > "%%i_%%j".txt
        :CONTINUE
        rem
    )
)

但是,当if语句首次为true时,它不会进一步迭代.我只得到不超过1_256.txt的文本文件.因此goto CONTINUE似乎有问题.我的批处理文件怎么了?

But when if statement is true for the first time, it does not iterate further. I only get text files upto 1_256.txt. So it seems that goto CONTINUE has a problem. What is wrong with my batch file?

推荐答案

goto :Label在代码()块内,就像for循环一样,破坏了块上下文,因此将:Label之后的所有内容都视为在街区之外.因此,您需要将if条件反转为不需要goto,如这是答案所示,或者将代码段与goto:Label进入子例程并像这样使用call:

goto :Label inside of a block of code () like a for loop breaks the block context, so everything after the :Label is treated as being outside of the block. So you need to invert the if condition to not need goto as ths's answer demonstrates, or you place the code fragment with goto and :Label into a subroutine and use call like this:

for %%i in (1, 2, 4, 8, 16, 32, 64, 128, 256) do (
    for %%j in (1, 2, 4, 8, 16, 32, 64, 128, 256) do (
        call :SUB %%i %%j
    )
)
exit /B

:SUB outer inner
if %1 gtr %2 goto CONTINUE
test.exe 0 %1 %2 100000 > "%1_%2.txt"
:CONTINUE
rem
exit /B

这篇关于“继续" Windows Batch中嵌套循环中的等效命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 03:26