Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

windows - 批处理脚本循环(Batch script loop)

I need to execute a command 100-200 times, and so far my research indicates that I would either have to copy/paste 100 copies of this command, OR use a for loop, but the for loop expects a list of items, hence I would need 200 files to operate on, or a list of 200 items, defeating the point.

(我需要执行一次命令100-200次,到目前为止,我的研究表明我要么必须复制/粘贴该命令的100个副本,要么使用for循环,但是for循环需要项列表,因此我将需要200个文件进行操作,或者需要200个项目的列表才能达到目的。)

I would rather not have to write a C program and go through the length of documenting why I had to write another program to execute my program for test purposes.

(我宁愿不必编写C程序,也无需花大量时间记录为什么我必须编写另一个程序来执行我的程序以进行测试。)

Modification of my program itself is also not an option.

(修改程序本身也不是一种选择。)

So, given a command, a , how would I execute it N times via a batch script?

(因此,给定命令a ,我如何通过批处理脚本执行N次?)

  ask by Tom J Nowell translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

for /l is your friend:

(for /l是您的朋友:)

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

Starts at 1, steps by one, and finishes at 100.

(从1开始,以1为步长,以100为结束。)

Use two % s if it's in a batch file

(如果在批处理文件中,请使用两个% s)

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

(which is one of the things I really really hate about windows scripting)

((这是我真正讨厌Windows脚本的事情之一))

If you have multiple commands for each iteration of the loop, do this:

(如果循环的每个迭代都有多个命令,请执行以下操作:)

for /l %x in (1, 1, 100) do (
   echo %x
   copy %x.txt z:whateveretc
)

or in a batch file

(或在批处理文件中)

for /l %%x in (1, 1, 100) do (
   echo %%x
   copy %%x.txt z:whateveretc
)

Key:

(键:)
/l denotes that the for command will operate in a numerical fashion, rather than operating on a set of files

(/l表示for命令将以数字方式操作,而不是对一组文件进行操作)
%x is the loops variable

(%x是循环变量)
(starting value, increment of value, end condition[inclusive] )

((起始值,值的增量,结束条件[含]))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...