使用 Windows Batch 脚本在 FOR 循环中计数

发布于 2024-12-06 05:20:33 字数 362 浏览 0 评论 0原文

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

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

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

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

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

り繁华旳梦境 2024-12-13 05:20:33

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

换句话说,在运行循环之前,%count% 被替换为其值 1

您需要的是这样的:

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

使用 ! 而不是 % 的延迟扩展将为您提供预期的行为。另请参阅此处


另请记住,setlocal/endlocal 实际上限制了内部更改的范围,以便它们不会泄漏。如果您想在 endlocal 之后使用 count ,则必须使用因您所遇到的问题而成为可能的“技巧”:

endlocal && set count=%count%

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

endlocal && set count=7

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

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

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

It's not working because the entire for loop (from the for to the final closing parenthesis, including the commands between those) is being evaluated when it's encountered, before it begins executing.

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

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/endlocal actually limit scope of things changed inside so that they don't leak out. If you want to use count after the endlocal, you have to use a "trick" made possible by the very problem you're having:

endlocal && set count=%count%

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

endlocal && set count=7

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

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

endlocal && set count=%count% && set something_else=%something_else%
骄兵必败 2024-12-13 05:20:33

for a = 1 到 100 步骤 1

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

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

for a = 1 to 100 step 1

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

    for /L %a in (1,1,100) Do echo %a 
断桥再见 2024-12-13 05:20:33

这是生成所有 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

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

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文