33

Is there a way to say something like

if %1 == 1 or %1 == 2

in a batch file? Or, even better, if I could specify a set of candidate values like

if %1 in [1, 2, 3, 4, ... 20]
MxLDevs
  • 19,048
  • 36
  • 123
  • 194

4 Answers4

41

The "and" turns out to be easy -- just not the syntax you expect: These 3 examples illustrate it.

In words: If 1==1 AND 2==2 Then echo "hello"

if 1==1 echo hello
hello

if 1==1 if 2==2 echo hello
hello

if 1==1 if 2==1 echo hello
(nothing was echoed)
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
Dana Forsberg
  • 411
  • 4
  • 2
26

One way to implement logical-or is to use multiple conditionals that goto the same label.

if %1 == 1 goto :cond
if %1 == 2 goto :cond
goto :skip
:cond
someCommand
:skip

To test for set membership, you could use a for-loop:

for %%i in (1 2 3 4 ... 20) do if %1 == %%i someCommand

Note that == is the string equality operator. equ is the numeric equality operator.

bk1e
  • 23,871
  • 6
  • 54
  • 65
  • @Joey: They usually act the same, but not always: `if 5 equ 05 echo yes` prints "yes" but `if 5 == 05 echo yes` does not. – bk1e May 23 '11 at 01:12
  • Ah, leading zeroes. With those `9 equ 09` fails since it tries interpreting them as octal. Nevermind the initial comment, then :-) – Joey May 23 '11 at 10:21
10

I know this is old, but I just wanted to let you know, that it indeed is possible, unlike the previous posts said. Basically you are tying two IF commands into one.

Syntax: IF equation (cmd if true)else command if false

Try this for two variables (to perform IF xx AND xx statement)

set varone=1
set vartwo=2

if %varone% equ 1 (if %vartwo% equ 2 (echo TRUE)else echo FALSE)else echo FALSE

with one Variable (to perform OR statement- note you can use more than one variable as well)

if %a% equ 1 (echo pass)else if %a equ 2 (echo pass)else echo false

You can substitute Echo pass/fail with your command

bill
  • 711
  • 1
  • 8
  • 19
  • just noticed this. Good to know that there is a way to do it (and may lead to other uses), but the syntax makes it look really cluttered! A for loop over a set of candidate values would be cleaner in this case. – MxLDevs Feb 05 '14 at 16:23
0

A bit late in the game, but nevertheless assuming if this might help anyone stumbling upon the question. The way I do this is using a combination of echo piped to findstr, this way:

(echo ":1: :2:" | findstr /i ":%1:" 1>nul 2>nul) && (
    echo Input is either 1 or 2
)

Since findstr is an external command, I recommend not using this inside a loop which may go through 1000's of iterations. If that is not the case, this should solve what you are attempting to do instead of using multiple ifs. Also, there is nothing special in the choice of ":", just use a delimiter which is unlikely to be part of the value in the %1.

Thanks to the rest of folks on pointing to another link which appears to have similar question, I will post this response there as well, just in case someone stumbles upon that question and doesn't quite reach here.

Arun
  • 2,493
  • 15
  • 12
  • Would this solution be scalable for large amounts of valid elements (eg: a list of extensions) – MxLDevs Nov 27 '13 at 18:17
  • @Keikoku: Just so that I understand better, for instance, in the situation outlined in the original question above, are you looking for input that can be 1, 2, 3,...so on (till a huge number)? It won't be feasible in that case. But certainly we can explore better way to fit that. A brief description of your situation can help - you can provide a link for your question, if you have already raised one. – Arun Nov 27 '13 at 21:41
  • I don't remember the problem I was working with when the question was opened. I think at some point I wanted to check what the input was and did something according to the input (and I had the choice between writing an exe or a batch file) – MxLDevs Nov 27 '13 at 22:06