1.

Solve : Help with .bat and IF / ELSE commands?

Answer»

Hey all

I'm trying to test a variable and if its correct open up another batch file, if its false go to another part of the original .bat file. Below is how Ive got my commands :

IF %VARIABLE% == 1 (
CALL TEST.BAT
)ELSE(
GOTO TEST2
)

But it generates this output without running the 2nd .bat file. PROMPT outputs "ELSE( was UNEXPECTED at this time". Ive tried changing the format but I cant get it to work.

Would this be the right way of doing it, or is there a better way.

Thanks, Alextry;

if /i "%variable%" EQU "1" (start test.bat) else goto test2


notice I added "/i" this will MAKE the if statement compare them as integers. I've also added the " " around the values we're comparing and I stuck it on one line cos I reakon it's easier to read.

I haven't tested it, but I hope it helps. Quote from: blastman on May 09, 2008, 08:30:06 AM

notice I added "/i" this will make the if statement compare them as integers.

No it won't; using if with the /i switch makes the comparison case-insensitive, or makes it ignore case if you prefer. Since numerical digits don't have "upper" or "lower" case, it will have no effect whatsoever.

Quote
I've also added the " " around the values we're comparing

Good practice

Quote
and I stuck it on one line cos I reakon it's easier to read.

Ditto

This is an alternative

Code: [Select]if "%variable%"=="1" (start test.bat) else goto test2
Quote from: Dias de verano on May 09, 2008, 10:50:00 AM
Quote from: blastman on May 09, 2008, 08:30:06 AM
notice I added "/i" this will make the if statement compare them as integers.

No it won't; using if with the /i switch makes the comparison case-insensitive, or makes it ignore case if you prefer. Since numerical digits don't have "upper" or "lower" case, it will have no effect whatsoever.


I may have miss understood the explaintion for the help option.

"and the /I switch, if SPECIFIED, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF."

and the bit that confused me...

"These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the stings are converted to numbers and a numeric comparison is performed"

I guess I thought that last section was part of the explation of the /I switch.

oh well.

as long as it works eh???

Quote from: blastman on May 09, 2008, 01:41:19 PM
the bit that confused me...

"These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the stings are converted to numbers and a numeric comparison is performed"

That means decimal (or lesser base) integers (the only numbers that don't contain punctuation or letters) are compared as numbers, everything else as strings. (Numeric comparisons are made as if all
the numbers were base ten, so comparing an octal and a decimal number would often give wrong results.)
Thanks for the reply guys and it works


Discussion

No Comment Found