Is there a way to write an IF OR IF conditional statement in a windows batch-file?
For example:
IF [%var%] == [1] OR IF [%var%] == [2] ECHO TRUE
Is there a way to write an IF OR IF conditional statement in a windows batch-file?
For example:
IF [%var%] == [1] OR IF [%var%] == [2] ECHO TRUE
Even if this question is a little older:
If you want to use
if cond1 or cond 2
- you should not use complicated loops or stuff like that.Simple provide both
ifs
after each other combined withgoto
- that's an implicit or.Without goto but an "inplace" action, you might execute the action 3 times, if ALL conditions are matching.
Never got exist to work.
I use if not exist g:xyz/what goto h: Else xcopy c:current/files g:bu/current There are modifiers /a etc. Not sure which ones. Laptop in shop. And computer in office. I am not there.
Never got batch files to work above Windows XP
While dbenham's answer is pretty good, relying on
IF DEFINED
can get you in loads of trouble if the variable you're checking isn't an environment variable. Script variables don't get this special treatment.While this might seem like some ludicrous undocumented BS, doing a simple shell query of
IF
withIF /?
reveals that,In regards to answering this question, is there a reason to not just use a simple flag after a series of evaluations? That seems the most flexible
OR
check to me, both in regards to underlying logic and readability. For example:Obviously, they can be any sort of conditional evaluation, but I'm just sharing a few examples.
If you wanted to have it all on one line, written-wise, you could just chain them together with
&&
like:Realizing this is a bit of an old question, the responses helped me come up with a solution to testing command line arguments to a batch file; so I wanted to post my solution as well in case anyone else was looking for a similar solution.
First thing that I should point out is that I was having trouble getting IF ... ELSE statements to work inside of a FOR ... DO clause. Turns out (thanks to dbenham for inadvertently pointing this out in his examples) the ELSE statement cannot be on a separate line from the closing parens.
So instead of this:
Which is my preference for readability and aesthetic reasons, you have to do this:
Now the ELSE statement doesn't return as an unrecognized command.
Finally, here's what I was attempting to do - I wanted to be able to pass several arguments to a batch file in any order, ignoring case, and reporting/failing on undefined arguments passed in. So here's my solution...
Note, this will only report on the first failed argument. So if the user passes in more than one unacceptable argument, they will only be told about the first until it's corrected, then the second, etc.
I don't think so. Just use two IFs and GOTO the same label:
The zmbq solution is good, but cannot be used in all situations, such as inside a block of code like a FOR DO(...) loop.
An alternative is to use an indicator variable. Initialize it to be undefined, and then define it only if any one of the OR conditions is true. Then use IF DEFINED as a final test - no need to use delayed expansion.
You could add the ELSE IF logic that arasmussen uses on the grounds that it might perform a wee bit faster if the 1st condition is true, but I never bother.
Addendum - This is a duplicate question with nearly identical answers to Using an OR in an IF statement WinXP Batch Script
Final addendum - I almost forgot my favorite technique to test if a variable is any one of a list of values. Initialize a test variable containing a delimitted list of acceptable values, and then use search and replace to test if your variable is within the list. This is very fast and uses minimal code for an arbitrarily long list. It does require delayed expansion (or else the CALL %%VAR%% trick). Also the test is CASE INSENSITIVE.
The above can fail if VAR contains
=
, so the test is not fool-proof.If doing the test within a block where delayed expansion is needed to access current value of VAR then
FOR options like "delims=" might be needed depending on expected values within VAR
The above strategy can be made reliable even with
=
in VAR by adding a bit more code.But now we have lost the ability of providing an ELSE clause unless we add an indicator variable. The code has begun to look a bit "ugly", but I think it is the best performing reliable method for testing if VAR is any one of an arbitrary number of case-insensitive options.
Finally there is a simpler version that I think is slightly slower because it must perform one IF for each value. Aacini provided this solution in a comment to the accepted answer in the before mentioned link
The list of values cannot include the * or ? characters, and the values and
%VAR%
should not contain quotes. Quotes lead to problems if the%VAR%
also contains spaces or special characters like^
,&
etc. One other limitation with this solution is it does not provide the option for an ELSE clause unless you add an indicator variable. Advantages are it can be case sensitive or insensitive depending on presence or absence of IF/I
option.