【问题标题】:Comparing variables inside a /F loop (BATCH)比较 /F 循环中的变量 (BATCH)
【发布时间】:2012-08-30 10:06:02
【问题描述】:

我有这个代码:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A counter=0
SET /A counter2=0

for /f %%h in (users.txt) do (
    set /a counter2=0
    set /a counter=!counter!+1

for /f %%i in (users.txt) do (
    set /a counter2=!counter2!+1
    IF !counter! gtr !counter2!
            echo !counter! and !counter2! 
    )
)

由于某种原因,我的 If 语句在那里出错。如果我把它划掉,一切都会很好。 我的语法有什么问题? 谢谢!

【问题讨论】:

    标签: if-statement for-loop batch-file


    【解决方案1】:

    EitanT 可能有您正在寻找的解决方案,但它并不能完全解释您的问题。

    如果您删除 IF 语句,则在调整缩进以更好地显示实际逻辑之后,您会得到这个。第二个循环在第一个循环内运行。

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    SET /A counter=0
    SET /A counter2=0
    
    for /f %%h in (users.txt) do (
      set /a counter2=0
      set /a counter=!counter!+1
    
      for /f %%i in (users.txt) do (
        set /a counter2=!counter2!+1
        echo !counter! and !counter2!
      )
    )
    

    当你输入 IF 语句时,你会得到这个不正确的代码

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    SET /A counter=0
    SET /A counter2=0
    
    for /f %%h in (users.txt) do (
      set /a counter2=0
      set /a counter=!counter!+1
    
      for /f %%i in (users.txt) do (
        set /a counter2=!counter2!+1
        IF !counter! gtr !counter2!
        echo !counter! and !counter2!
      )
    )
    

    IF 语句不完整 - 如果为真,您没有告诉它该怎么做。

    如果您希望 ECHO 成为 IF 的一部分,那么您需要做 3 件事中的 1 件事:

    1) 将 ECHO 附加到 IF 语句

    IF !counter! gtr !counter2! echo !counter! and !counter2!
    


    2) 使用续行将 IF 和 ECHO 行变成一个逻辑行

    IF !counter! gtr !counter2!^
    echo !counter! and !counter2!
    


    3) 添加另一组括号。注意左括号必须和 IF 在同一行,并且前面必须有一个空格。

    IF !counter! gtr !counter2! (
      echo !counter! and !counter2!
    )
    


    帮助系统描述了 IF 的正确语法。在命令行中输入 HELP IFIF /? 以获取帮助。

    请注意,我发布的代码逻辑与 EitanT 解决方案不同。我不确定哪个是正确的。像大多数编程语言一样,缩进不会影响逻辑,它的存在是为了让人们更清楚地了解逻辑是什么。您的原始缩进表示 EitanT 提供的逻辑。我忽略了缩进并提供了计算机看到的逻辑。

    顺便说一句 - 您不需要在 SET /A 语句中扩展变量。以下工作正常:

    set /a counter=counter+1
    

    更好的是,您可以使用递增赋值运算符:

    set /a counter+=1
    

    SET /A 还支持在一个语句中进行多个赋值:

    set /a counter2=0, counter+=1
    

    您不需要在顶部初始化 counter2,因为您也在第一个循环中进行了初始化。

    这是使用我根据您现有括号看到的逻辑的最终代码,忽略您的缩进:

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    SET /A counter=0
    
    for /f %%h in (users.txt) do (
      set /a counter2=0, counter+=1
    
      for /f %%i in (users.txt) do (
        set /a counter2+=1
        IF !counter! gtr !counter2! echo !counter! and !counter2!
      )
    )
    

    【讨论】:

      【解决方案2】:

      我可以发现两个问题:

      1) 第一个for 循环没有右括号:

      for /f %%h in (users.txt) do (
          set /a counter2=0
          set /a counter=!counter!+1
      )   <---------------------------------- You're missing this ")"!
      

      2) 在第二个循环中,if 语句缺少左括号:

      IF !counter! gtr !counter2! (   <------ You're missing this "("!
              echo !counter! and !counter2! 
      )
      

      希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        您的if 语句应该全部在一行中,或者包含在() 中。

        这里是一个更正:

        @echo off
        SETLOCAL ENABLEDELAYEDEXPANSION
        
        SET /A counter=0
        SET /A counter2=0
        
        for /f %%h in (users.txt) do (
          set /a counter2=0
          set /a counter=!counter!+1
        
          for /f %%i in (users.txt) do (
            set /a counter2=!counter2!+1
            IF !counter! gtr !counter2! (
              echo !counter! and !counter2! 
            )
          )
        )
        

        正确的缩进可以帮助您找出包围错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-13
          • 1970-01-01
          相关资源
          最近更新 更多