Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: IF statement with variable  (Read 2597 times)

0 Members and 1 Guest are viewing this topic.

fjwolz19

  • Guest
IF statement with variable
« on: May 29, 2007, 06:38:56 AM »
Hello,
I am trying to create a bat file that will do something based on the last octet of the ip of the machine.  I have already made something that gets the ip (from the registry) takes the last three digits and stores it as a variable (ip).  Now I am having trouble with the IF statements.  To make it easier I just started at the highest group of ips, used greater than (GTR) and worked my way down.  In the labels I exit the script after it does what I need so it is only possible to enter one IF statement.  Each if statement sends it to the appropriate label to do what I need that computer to do.

IF %ip% GTR 200
GOTO group1
END

IF %ip% GTR 190
GOTO group2
END

......and so on......

group1:
......do stuff.......
EXIT

group2:
.....do stuff........
EXIT

I am pretty sure that my problem is with the IF statements, but I am not sure what I am doing wrong.  Thank you in advance for reading through this and I look forward to any guidance anyone can offer.

contrex

  • Guest
Re: IF statement with variable
« Reply #1 on: May 29, 2007, 07:06:29 AM »
I am pretty sure that my problem is with the IF statements, but I am not sure what I am doing wrong.  Thank you in advance for reading through this and I look forward to any guidance anyone can offer.

Your problem is, that code is not batch language. It is some other language maybe not yet invented.

There are 2 problems. The IFs and the labels.

(1) IF statements in batch are usually like this on one line. You don't have "END".

IF %ip% GTR 200 goto group1
IF %ip% GTR 190 goto group2

(2) You got the labels back-to-front. The colon goes BEFORE the label name

:group1
.....do stuff....
EXIT

:group2
.....do stuff....
EXIT

It is not compulsory to put *anything* in a batch file in capital letters.

If you want a multiline if, use braces (parentheses) like this

if %ip% gtr 200 (
echo it was more than 200
echo another line
goto group1
)

:group1

Or use & like this

if %ip% gtr 200 echo more than 200 & echo some more text & goto group1