JustAGuy
JustAGuy

Reputation: 5941

Comparing variables inside a /F loop (BATCH)

I have this code:

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

For some reason I the If statement in there errors. If I cross it out it all runs just fine. What's wrong with my syntax? Thanks!

Upvotes: 0

Views: 4088

Answers (3)

James K
James K

Reputation: 4055

Your if statement should either be all on one line, or enclosed in ().

Here is a correction:

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

Correct indentation can help you track down bracketing errors.

Upvotes: 1

dbenham
dbenham

Reputation: 130809

EitanT likely has the solution you are looking for, but it doesn't fully explain your problem.

If you remove the IF statement you get this, after the indentation is adjusted to better show the actual logic. The 2nd loop runs inside the 1st.

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

When you put in the IF statement you get this improper code

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

The IF statement is incomplete - you didn't tell it what to do if true.

If you want the ECHO to be part of the IF then you need to do 1 of 3 things:

1) Append the ECHO to the IF statement

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


2) Use line continuation to turn the IF and ECHO lines into one logical line

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


3) Add another set of parentheses. Note the opening parenthesis must be on the same line as the IF, and there must be a space in front of it.

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


The help system describes the proper syntax for IF. Type HELP IF or IF /? from a command line to get help.

Notice the logic of the code I have posted is different than the EitanT solution. I'm not sure which is correct. Like most programming languages, the indentation does not affect the logic, it is there to make it more obvious to humans what the logic is. Your original indentation indicates the logic that EitanT provided. I ignored the indentation and provided the logic that the computer sees.

BTW - you do not need to expand variables in SET /A statements. The following works fine:

set /a counter=counter+1

Even better, you can use the incrementing assignment operator:

set /a counter+=1

SET /A also supports multiple assignments in one statement:

set /a counter2=0, counter+=1

You do not need to initialize counter2 at the top since you also do it within your 1st loop.

Here is the final code using the logic that I see based on your existing parentheses, ignoring your indentation:

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

Upvotes: 2

Eitan T
Eitan T

Reputation: 32920

I can spot two problems:

1) The first for loop doesn't have a closing parenthesis:

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

2) In the second loop, the if statement lacks an opening parenthesis:

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

Hope this helps!

Upvotes: 2

Related Questions