使用 Windows 批处理脚本在 FOR 循环中计数

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

Counting in a FOR loop using Windows Batch script

windowsbatch-filecmd

提问by djangofan

Can anyone explain this? I am able to count in a loop using the Windows command prompt, using this method:

谁能解释一下?我可以使用 Windows 命令提示符循环计数,使用以下方法:

SET /A XCOUNT=0
:loop
SET /A XCOUNT+=1
echo %XCOUNT%
IF "%XCOUNT%" == "4" (
  GOTO end
) ELSE (
  GOTO loop
)
:end

But this method does not work (it prints out "1" for each line in the file). It acts like the variable is out of scope:

但是这种方法不起作用(它为文件中的每一行打印出“1”)。它就像变量超出范围一样:

SET /A COUNT=1
FOR /F "tokens=*" %%A IN (config.properties) DO (
  SET /A COUNT+=1
  ECHO %COUNT%
)

回答by paxdiablo

It's not working because the entireforloop (from the forto the final closing parenthesis, includingthe commands between those) is being evaluated when it's encountered, beforeit begins executing.

它不起作用,因为整个for循环(从for到最后的右括号,包括它们之间的命令)在遇到它时,它开始执行之前都会被评估。

In other words, %count%is replaced with its value 1before running the loop.

换句话说,在运行循环之前%count%用它的值替换1

What you need is something like:

你需要的是这样的:

setlocal enableextensions enabledelayedexpansion
set /a count = 1
for /f "tokens=*" %%a in (config.properties) do (
  set /a count += 1
  echo !count!
)
endlocal

Delayed expansion using !instead of %will give you the expected behaviour. See also here.

使用!而不是延迟扩展%会给你预期的行为。另请参见此处



Also keep in mind that setlocal/endlocalactually limit scope of things changed inside so that they don't leak out. If you want to use countafterthe endlocal, you have to use a "trick" made possible by the very problem you're having:

还要记住,setlocal/endlocal实际上限制了内部变化的范围,以免它们泄漏。如果您想在count之后使用endlocal,则必须使用因您遇到的问题而成为可能的“技巧”:

endlocal && set count=%count%

Let's say counthas become 7 within the inner scope. Because the entire command is interpreted before execution, it effectively becomes:

假设count在内部范围内已变为 7。因为整个命令在执行之前被解释,它实际上变成:

endlocal && set count=7

Then, when it's executed, the inner scope is closed off, returning countto it's original value. But, since the setting of countto seven happens in the outer scope, it's effectively leaking the information you need.

然后,当它被执行时,内部作用域被关闭,返回count到它的原始值。但是,由于设置count为 7 发生在外部作用域中,因此它有效地泄漏了您需要的信息。

You can string together multiple sub-commands to leak as much information as you need:

您可以将多个子命令串在一起以根据需要泄漏尽可能多的信息:

endlocal && set count=%count% && set something_else=%something_else%

回答by sujith

for a = 1 to 100 step 1

对于 a = 1 到 100 步骤 1

Command line in Windows . Please use %%a if running in Batch file.

Windows 中的命令行。如果在批处理文件中运行,请使用 %%a。

    for /L %a in (1,1,100) Do echo %a 

回答by Steve Hull

Here is a batch file that generates all 10.x.x.x addresses

这是一个生成所有10.xxx地址的批处理文件

@echo off

SET /A X=0
SET /A Y=0
SET /A Z=0

:loop
SET /A X+=1
echo 10.%X%.%Y%.%Z%
IF "%X%" == "256" (
 GOTO end
 ) ELSE (
 GOTO loop2
 GOTO loop
 )


:loop2
SET /A Y+=1
echo 10.%X%.%Y%.%Z%
IF "%Y%" == "256" (
  SET /A Y=0
  GOTO loop
  ) ELSE (
   GOTO loop3
   GOTO loop2
 )


:loop3

SET /A Z+=1
echo 10.%X%.%Y%.%Z%
IF "%Z%" == "255" (
  SET /A Z=0
  GOTO loop2
 ) ELSE (
   GOTO loop3
 )

:end