Help with the "gtr" "lss" etc arguments in the "if" command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Help with the "gtr" "lss" etc arguments in the "if" command

#1 Post by Rileyh » 12 Sep 2011 22:14

Hi,
Could someone give me a simple sintax overview to the various arguments involving "gtr" "lss" etc in the "if" command.

Any help would be greatly appreciated,
Rileyh

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: Help with the "gtr" "lss" etc arguments in the "if" comm

#2 Post by phillid » 12 Sep 2011 23:38

Sure, always happy to help :P

Basically, the arguments you're talking about include:

EQU - EQUAL to
NEQ - NOT EQUAL to
LSS - LESS than
LEQ - LESS than OR EQUAL to
GTR - GREATER than
GEQ - GREATER than or EQUAL to

These are called comparison operators. The basic syntax of the if statement is as follows:

Code: Select all

if [optional 'not'] [expression1] [comparison operator] [expression 2] ([code to run if true])

If we wanted to see if say, %var% was equal to the value '2', we could use this:

Code: Select all

if "%var%" EQU "2" echo Var was equal to 2

In this case, if we set %var% to 2 before this statement, the statement would be true, so the message 'Var was equal to 2' would be put on the screen. If we didn't set %var% to 2 then nothing would happen.

Another example; if we wanted to see if %var1% was greater than or equal to '5', we would use this:

Code: Select all

if "%var1%" GEQ "5" echo Var1 was greater than or equal to 5


Please note that the quotes are not needed unless you expect an empty variable or a variable containing multiple words, so it's normally a good idea to include them

The things you are comparing can be variables or hard-coded values. You can put any one operator between your values to compare them in the way you want. Have a play around with comparison operators and see what you discover :D!


That's got to be the longest post I've made :D
For more detailed help, type if /? at the command line


-- Phillid

Post Reply