If statement 比较a/F循环内的变量(批处理)

If statement 比较a/F循环内的变量(批处理),if-statement,for-loop,batch-file,If Statement,For Loop,Batch File,我有以下代码: @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 !counte

我有以下代码:

@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语句中发现了错误。如果我把它划掉,一切都会好的。 我的语法有什么问题?
谢谢

我可以发现两个问题:

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

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

希望这有帮助

我可以发现两个问题:

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

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

希望这有帮助

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语句不完整-如果为true,您没有告诉它该怎么做

如果希望回声成为If的一部分,则需要执行以下三项操作之一:

1) 将ECHO追加到IF语句

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

2)使用线路延续将IF和回波线路转换为一条逻辑线路

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

3)添加另一组括号。注意,左括号必须与IF位于同一行,且前必须有空格

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

帮助系统描述了IF的正确语法。从命令行键入
HELP(如果
IF/?
)以获取帮助

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

顺便说一句,您不需要在SET/A语句中展开变量。以下工作很好:

set /a counter=counter+1
更好的是,您可以使用递增赋值运算符:

set /a counter+=1
SET/A还支持一条语句中的多个赋值:

set /a counter2=0, counter+=1
您不需要在顶部初始化计数器2,因为您也在第一个循环中进行初始化

下面是使用基于现有括号的逻辑的最终代码,忽略缩进:

@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!
  )
)

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语句不完整-如果为true,您没有告诉它该怎么做

如果希望回声成为If的一部分,则需要执行以下三项操作之一:

1) 将ECHO追加到IF语句

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

2)使用线路延续将IF和回波线路转换为一条逻辑线路

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

3)添加另一组括号。注意,左括号必须与IF位于同一行,且前必须有空格

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

帮助系统描述了IF的正确语法。从命令行键入
HELP(如果
IF/?
)以获取帮助

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

顺便说一句,您不需要在SET/A语句中展开变量。以下工作很好:

set /a counter=counter+1
更好的是,您可以使用递增赋值运算符:

set /a counter+=1
SET/A还支持一条语句中的多个赋值:

set /a counter2=0, counter+=1
您不需要在顶部初始化计数器2,因为您也在第一个循环中进行初始化

下面是使用基于现有括号的逻辑的最终代码,忽略缩进:

@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!
  )
)

您的
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 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! 
    )
  )
)
正确的缩进可以帮助您跟踪括号错误