views:

170

answers:

2

in DOS batch files, In an IF statement, is it possible to combine two or more conditions using AND or OR ? I was not able to find any documentation for that

Edit - help if and the MS docs say nothing about using more than one condition in an if.

I guess a workaround for AND would be to do

if COND1 (
  if COND2 (
    cmd
  )
)

but this is exactly what I'm trying to avoid.

+2  A: 

No, there is no easier way.

For and you can also just chain them without introducing blocks:

if COND1 if COND2 ...

which frankly isn't any worse than

if COND1 and COND2 ...

However, for or it gets uglier, indeed:

set COND=
if COND1 set COND=1
if COND2 set COND=1
if defined COND ...

or:

if COND1 goto :meh
if COND2 goto :meh
goto :meh2
:meh
...
:meh2

I once saw a batch preprocessor which used C-like syntax for control flow and batch stuff in between and then converted suh conditionals into multiple jumps and checks. However, that thing was for DOS batch files and was of little to no use in a Windows environment.

Joey
A: 

As long as command extensions are enabled, you can use compare-operators.

This is directly pasted from "help if":

If Command Extensions are enabled IF changes as follows:

IF [/I] string1 compare-op string2 command
IF CMDEXTVERSION number command
IF DEFINED variable command

where compare-op may be one of:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

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. These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed.

Vicky
So, where are the logical `and` and `or` the OP asked about? The question wasn't about simple comparison conditions.
Joey
@Johannes Rössel: doh, sorry, my bad. I need to read more carefully.
Vicky
@Vicky yes you do.
shoosh