1.

Solve : Using if else in batch scripting?

Answer»

How to check 3 conditions from batch scripting

ex:-

if (condition1) & (condition2) are true do this

else do this
How about nesting ifs? Something LIKE:

Code: [Select]IF condition1 (
IF condition2 command
) ELSE command

Two-Eyes %It depends. Are the three tests know to be valid tests? That is, do we already know that the test will give a True or False without raising an error.

Nested IF statements are a good idea to prevent an invalid test problem. An example of this is when you need to test for the presence of a file and if true USE the file in some further test.You can do simple if - else tests in batch; there is no THEN keyword. You KEEP the IF code and the ELSE code apart with parentheses. Remember that (a) the closing parenthesis of the code block to be executed if the initial IF test is passed, (b) the ELSE keyword, (c) the opening parenthesis of the code block to be executed if the ELSE is triggered, MUST ALL BE ON THE SAME LINE.

Code: [Select]IF "%animal%"=="dog" (
echo canine
echo likes bones
) ELSE (
echo not canine
echo might not like bones
)
..To do Boolean tests you have to do a bit of trickery

Condition 1: animal is dog
Condition 2: COLOUR is red

Code: [Select]REM AND
set bool=0
IF "%animal%"=="dog" set /a bool=%bool%+1
IF "%colour%"=="red" set /a bool=%bool%+1
if %bool% EQU 2 (
echo condition 1 AND condition 2 are both true
) ELSE (
echo condition 1 AND condition 2 are NOT both true
)

REM OR
set bool=0
IF "%animal%"=="dog" set /a bool=%bool%+1
IF "%colour%"=="red" set /a bool=%bool%+1
if %bool% GEQ 1 (
echo Either condition 1 OR condition 2 is true
) ELSE (
Neither condition 1 nor condition 2 is true
)

To avoid spoiling all your fun, I'll leave you to figure out NOT using this approach...Did you leave out an echo?
Here is XOR
Code: [Select]if %bool% EQU 1 (
echo condition 1 XOR condition 2 is true
) ELSE (
echo condition 1 XOR condition 2 is false
)Which is what people think OR means!Quote from: Geek-9pm on September 16, 2009, 02:44:47 PM

Did you leave out an echo?

Yup

Exclusive OR Venn diagram: the red part is true


XOR is one or the other but not both.

A XOR B = (NOT (A=B) and (A OR B))Quote
Care should be taken when converting an English sentence into a formal Boolean statement. Many English sentences have imprecise meanings, e.g. "All that glisters is not gold,"[1] which could mean that "NOTHING that glisters is gold" or "some things which glister are not gold".

AND and OR can also be used interchangeably in English, in certain cases:

* "I always carry an umbrella for when it rains and snows."

* "I always carry an umbrella for when it rains or snows."

* " I never walk in the rain or snow."

http://en.wikipedia.org/wiki/Boolean_logic

"I always carry an umbrella for when it rains and snows."

this is valid because there is an implication:

"I always carry an umbrella for when it rains and for when it snows."Quote from: BC_Programmer on September 16, 2009, 08:52:43 PM
"I always carry an umbrella for when it rains and snows."

this is valid because there is an implication:

"I always carry an umbrella for when it rains and for when it snows."
OK, Maybe we can close this thread if you will just write a batch file using the IF...ELSE structure to demonstrate the above logic.
Thanks Salmon Trout
-----
IF "%animal%"=="dog" set /a bool=%bool%+1
IF "%colour%"=="red" set /a bool=%bool%+1
if %bool% EQU 2 (
echo condition 1 AND condition 2 are both true
) ELSE (
echo condition 1 AND condition 2 are NOT both true
)
---------
This thing is really working great, now i can allow any no. of conditions in a IF loop.
initially i thought like "C" language i can enter elseif condition.
Thanks again.


Discussion

No Comment Found