Page 1 of 1

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

Posted: 12 Sep 2011 22:14
by Rileyh
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

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

Posted: 12 Sep 2011 23:38
by phillid
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