windows 批处理脚本循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2591758/
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 07:14:56  来源:igfitidea点击:

Batch script loop

windowsloopsbatch-file

提问by Tom J Nowell

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 forloop, but the forloop 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. Modification of my program itself is also not an option.

我宁愿不必编写 C 程序并详细记录为什么我必须编写另一个程序来执行我的程序以进行测试。修改我的程序本身也不是一种选择。

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

那么,给定一个命令,a我将如何N通过批处理脚本执行它?

Note: I don't want an infinite loop

注意:我不想要无限循环

For example, here is what it would look like in Javascript:

例如,这是在 Javascript 中的样子:

var i;
for (i = 0; i < 100; i++) {
  console.log( i );
} 

What would it look like in a batch script running on Windows?

在 Windows 上运行的批处理脚本会是什么样子?

回答by Jon

for /lis 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 开始,一步一步,到 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:\whatever\etc
)

or in a batch file

或在批处理文件中

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

Key:
/ldenotes that the forcommand will operate in a numerical fashion, rather than operating on a set of files
%xis the loops variable
(starting value, increment of value, end condition[inclusive] )

键:
/l表示该for命令将以数字方式操作,而不是对一组文件进行操作,
%x即循环变量
(起始值、值的增量、结束条件[包括])

回答by Olivier Faucheux

And to iterate on the files of a directory:

并迭代目录的文件:

@echo off 
setlocal enableDelayedExpansion 

set MYDIR=C:\something
for /F %%x in ('dir /B/D %MYDIR%') do (
  set FILENAME=%MYDIR%\%%x\log\IL_ERROR.log
  echo ===========================  Search in !FILENAME! ===========================
  c:\utils\grep motiv !FILENAME!
)

You must use "enableDelayedExpansion" and !FILENAME! instead of $FILENAME$. In the second case, DOS will interpret the variable only once (before it enters the loop) and not each time the program loops.

您必须使用“enableDelayedExpansion”和 !FILENAME! 而不是 $FILENAME$。在第二种情况下,DOS 只会解释变量一次(在它进入循环之前),而不是每次程序循环时。

回答by FluorescentGreen5

Template for a simple but counted loop:

一个简单但计数的循环的模板:

set loopcount=[Number of times]
:loop
[Commands you want to repeat]
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop

Example: Say "Hello World!" 5 times:

示例:说“Hello World!” 5次:

@echo off
set loopcount=5
:loop
echo Hello World!
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause

This example will output:

此示例将输出:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Press any key to continue . . .

回答by BlazeLP

You could also try this instead of a forloop:

你也可以试试这个而不是for循环:

set count=0
:loop
set /a count=%count%+1
(Commands here)
if %count% neq 100 goto loop
(Commands after loop)

It's quite small and it's what I use all the time.

它很小,是我一直使用的。

回答by Mechaflash

Or you can decrement/increment a variable by the number of times you want to loop:

或者,您可以按要循环的次数递减/递增变量:

SETLOCAL ENABLEDELAYEDEXPANSION
SET counter=200
:Beginning
IF %counter% NEQ 0 (
echo %x
copy %x.txt z:\whatever\etc
SET /A counter=%counter%-1
GOTO Beginning
) ELSE (
ENDLOCAL
SET counter=
GOTO:eof

Obviously, using FOR /Lis the highway and this is the backstreet that takes longer, but it gets to the same destination.

显然,使用的FOR /L是高速公路,这是需要更长的时间的后街,但它会到达相同的目的地。

回答by Marcus Culver

You could do something to the following effect avoiding the FOR loop.

您可以对以下效果做一些事情来避免 FOR 循环。

set counter=0
:loop
echo "input commands here"
SET /A counter=%counter%+1
if %counter% GTR 200
(GOTO exit) else (GOTO loop)
:exit
exit

回答by Rakesh Chaudhari

Very basic way to implement looping in cmd programming using labels

使用标签在 cmd 编程中实现循环的非常基本的方法

@echo off
SET /A "index=1"
SET /A "count=5"
:while
if %index% leq %count% (
   echo The value of index is %index%
   SET /A "index=index + 1"
   goto :while
)

回答by zask

You can do this without a forstatement ^.^:

您可以在没有for声明的情况下执行此操作^.^:

@echo off
:SPINNER
SET COUNTP1=1

:1
CLS
 :: YOUR COMMAND GOES HERE
IF !COUNTP1! EQU 200 goto 2

SET COUNTP1=1
) ELSE (
SET /A COUNTP1+=1
)
goto 1

:2
:: COMMAND HAS FINISHED RUNNING 200 TIMES

It has basic understanding. Just give it a test. :P

它有基本的了解。给它一个测试。:P

回答by Carl Smotricz

DOS doesn't offer very elegant mechanisms for this, but I think you can still code a loop for 100 or 200 iterations with reasonable effort. While there's not a numeric forloop, you can use a character string as a "loop variable."

DOS 没有为此提供非常优雅的机制,但我认为您仍然可以通过合理的努力为 100 或 200 次迭代编写循环。虽然没有数字for循环,但您可以使用字符串作为“循环变量”。

Code the loop using GOTO, and for each iteration use SET X=%X%@to add yet another @sign to an environment variable X; and to exit the loop, compare the value of X with a string of 100 (or 200) @signs.

使用 GOTO 对循环进行编码,并在每次迭代中向环境变量 XSET X=%X%@添加另一个@符号;并退出循环,将 X 的值与 100(或 200)个@符号的字符串进行比较。

I never said this was elegant, but it should work!

我从来没有说过这很优雅,但它应该有效!

回答by Josh Girouard

Not sure if an answer like this has already been submitted yet, but you could try something like this:

不确定是否已经提交了这样的答案,但您可以尝试以下操作:

@echo off
:start
set /a var+=1
if %var% EQU 100 goto end
:: Code you want to run goes here
goto start

:end
echo var has reached %var%.
pause
exit

The variable %var% will increase by one until it reaches 100 where the program then outputs that it has finished executing. Again, not sure if this has been submitted or something like it, but I think it may be the most compact.

变量 %var% 将增加 1,直到它达到 100,然后程序输出它已完成执行。同样,不确定是否已提交或类似的内容,但我认为它可能是最紧凑的。