通过命令行在范围上进行 Windows 批处理 FOR 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15577492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:22:49  来源:igfitidea点击:

Windows batch FOR loop on range through command line

windowsbatch-filefor-loopcmd

提问by Kirby

I want to perform an operation multiple times from a command window. Common sense tells me that a FOR loop should be able to handle this. Sure enough, if I want to execute, say, myProg.exe, I can open a command window and use:

我想从命令窗口多次执行操作。常识告诉我 FOR 循环应该能够处理这个问题。果然,如果我想执行,比如说myProg.exe,我可以打开一个命令窗口并使用:

C:\> FOR %i in (1 2 3) DO myProg.exe

Easy.

简单。

But what if I want to execute myProg.exe1000 times? I want to specify a range in the FOR loop, but I'm having trouble seeing how to do this.

但是如果我想执行myProg.exe1000 次呢?我想在 FOR 循环中指定一个范围,但我无法看到如何执行此操作。

Intuitively, it seems like I should be able to do something like one of the following:

直觉上,我似乎应该能够执行以下操作之一:

C:\> FOR %i in (1 to 1000) DO myProg.exe
C:\> FOR %i in (1-1000) DO myProg.exe

But, of course, this doesn't work. The FOR loop interprets the list as 3 tokens and 1 token, respectively, so myProg.exeis only executed 3 times and 1 time, respectively.

但是,当然,这行不通。FOR 循环将列表分别解释为 3 个标记和 1 个标记,因此分别myProg.exe只执行了 3 次和 1 次。



Batch File Solution

批处理文件解决方案

It'd probably be easy to write some sort of batch (.bat) file:

编写某种批处理(.bat)文件可能很容易:

SET COUNT=0
:MyLoop
    IF "%COUNT%" == "1000" GOTO EndLoop
    myProg.exe
    SET /A COUNT+=1
    GOTO MyLoop
:EndLoop

But isn't there an easy way to do this from the command line?

但是没有一种简单的方法可以从命令行执行此操作吗?

回答by Jeremy

You can use the /l tag in your statement to make it loop through a set of numbers.

您可以在语句中使用 /l 标记使其循环遍历一组数字。

eg.

例如。

C:\> FOR /l %i in (1,1,1000) DO myProg.exe

This says loop through the range, starting at 1, stepping 1 at a time, until 1000

这表示循环遍历范围,从 1 开始,一次步进 1,直到 1000

http://ss64.com/nt/for_l.html

http://ss64.com/nt/for_l.html

回答by user2106965

for /l %%i in (1,1,100) do echo %%i

for /l %%i in (1,1,100) do echo %%i

add another % sign before i to work

在我开始工作之前添加另一个 % 符号