计数器与IF语句在Windows批处理文件里面递增FOR循环 [英] Counter not incrementing with IF statement inside FOR loop in Windows Batch File

查看:327
本文介绍了计数器与IF语句在Windows批处理文件里面递增FOR循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的for循环,我创建倒不工作我期待的方式批处理文件内。在通过一个文本文件的行循环迭代,并在每次迭代中,我使用IF语句使当前行的快速比较,并做了一些工作,如果它的值为true。这里是code:

I have a simple FOR loop inside of a batch file I'm creating that isn't quite working the way I'm expecting. The FOR loop iterates through lines of a text file, and on each iteration, I use an IF statement to make a quick comparison on the current line, and do some work if it evaluates to true. Here is the code:

SETLOCAL ENABLEDELAYEDEXPANSION
set /a sitecounts=1
set /a input=34
FOR /F "tokens=1,2 delims=^/" %%G IN (file.txt) DO (
IF %sitecounts% == %input% (set /a selectedsitepath=%logfilepath%W3SVC%%H)
set /a sitecounts=!sitecounts!+1
echo !sitecounts!
)

我遇到一个奇怪的问题,其中,在 sitecounts 变量(我的柜台),实际上每个循环(我呼应了与最后一条语句计数证明这一点)适当增加 - 但到下一个循环的%sitecounts%仍显示它有1的价值,这是初始值。

I'm running into a weird issue where, the sitecounts variable (my counter) actually increments properly with each loop (I prove this by echoing out the count with the last statement) -- BUT upon the next iteration %sitecounts% still shows that it has the value of '1', which was the initial value.

如果我改变code为:

If I change the code to:

IF !sitecounts! == ...

...那么IF语句似乎把该值作为纯粹的文字!我设置为ENABLEDELAYEDEXPANSION循环中处理的变量,但不知道下一步该怎么做。任何帮助AP preciated!

...then the IF statement appears to treat that value as pure TEXT! I've set ENABLEDELAYEDEXPANSION to handle variables within loops, but not sure what to do next. Any help is appreciated!

推荐答案

这一次,我实际测试过它....

This time I actually tested it....

更改行

IF %sitecounts% == %input% (set /a selectedsitepath=%logfilepath%W3SVC%%H)

IF !sitecounts! == %input% (set selectedsitepath=%logfilepath%W3SVC%%H)


  • %sitecounts%必须!sitecounts!以进行评估

  • 设置/一selectedsitepath =%LOGFILEPATH%W3SVC %% ^ h 不起作用,因为它不是一个数字的前pression:

    • %sitecounts% needs to be !sitecounts! in order to be evaluated
    • set /a selectedsitepath=%logfilepath%W3SVC%%H doesn't work because it is not a numerical expression:

      / A开关指定字符串等号右边
        是评估数值前pression。

      The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated.

      只是省略开关( / P> / code>是行不通两者都不是,这是一个提示)

      Just omit the switch (/p is not going to work neither, that's for a prompt)

      这篇关于计数器与IF语句在Windows批处理文件里面递增FOR循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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