Windows批处理FOR循环通过命令行在范围内 [英] Windows batch FOR loop on range through command line

查看:646
本文介绍了Windows批处理FOR循环通过命令行在范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

容易

但是,如果我想执行myProg.exe 1000次怎么办?我想在FOR循环中指定一个范围,但是在查看如何执行操作时遇到了麻烦.

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

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

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

但是,这当然不起作用. FOR循环将列表分别解释为3个令牌和1个令牌,因此myProg.exe仅分别执行3次和1次.

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

批处理文件解决方案

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

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

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?

推荐答案

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

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

例如

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

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

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

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

这篇关于Windows批处理FOR循环通过命令行在范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆