12

Can I specify multiple conditions with "or"/"and" in batch file if block?

If not that complex, can I at least use something like:

if value1 < value < value2

Basically my purpose is to check whether current system time falls in a certain interval(2.05 AM and 7.55 AM to be precise) and if it does, to execute certain commands.

tumchaaditya
  • 1,267
  • 7
  • 19
  • 49

4 Answers4

33

Adding to dbenham's answer, you can emulate both logical operators (AND, OR) using a combination of if and goto statements.

To test condition1 AND codition2:

    if <condition1> if <condition2> goto ResultTrue

:ResultFalse
REM do something for a false result
    goto Done

:ResultTrue
REM do something for a true result

:Done

To test condition1 OR codition2:

    if <condition1> goto ResultTrue
    if <condition2> goto ResultTrue

:ResultFalse
REM do something for a false result
    goto Done

:ResultTrue
REM do something for a true result

:Done

The labels are of course arbitrary, and you can choose their names as long as they are unique.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • I used to write `if c1 if c2 goto AndTrue` in this (clearer) way: `set and=if`, `if c1 %and% c2 goto AndTrue`. You must note that these constructs can NOT have ELSE part!!! – Aacini Jun 03 '12 at 19:09
  • @Aacini my constructs have the "else" case in the `ResultFalse` section. – Eitan T Jun 03 '12 at 21:25
  • I see no benefit to using GOTO for the AND simulation - you can simply string the IFs together and put the ELSE with the last IF. But GOTO is a good solution for OR. Another option is to use a temporary variable: `set "OR=" & ((if set "OR=1") & if set "OR=1") & if defined OR (echo TRUE) else (echo FALSE)`. One reason to use this method instead of GOTO would be if your OR test is within a parenthesized block - You can't GOTO a label within a parenthesized block of code. – dbenham Jun 03 '12 at 22:39
  • @dbenham you really have something against `goto` in batch, huh? :) – Eitan T Jun 03 '12 at 22:41
  • 1
    Whoops - I have two misstatements in my previous comment. The last line should read "You ***shouldn't*** GOTO a label within a parenthesized block". It can be done, but is likely to give results other than what you want, and is confusing. The other deals with using ELSE when simulating AND with concatenated IF statements - it doesn't work! I owe an apology to EitanT - there definitely is an advantage of the GOTO solution if you want to use ELSE. There is also a non GOTO solution for AND with ELSE using a temporary variable, much like I did with OR. – dbenham Jun 03 '12 at 22:52
9

There are no logic operators in batch. But AND is easy to mimic with two IF statements

if value1 lss value if value lss value2 REM do something

Batch IF statements don't know how to compare times. Batch IF knows how to compare integers and strings.

One option is to convert the times into minutes or seconds past midnight.

The other option is to format the time with both hours, minutes (and seconds if needed) to be 2 digits wide each (0 prefixed as needed). The hours should be 24 hour format (military time).

dbenham
  • 127,446
  • 28
  • 251
  • 390
1

You can break your condition into 2 and use if and if for setting 2 conditions

if %value% GTR %value1% (
echo Value is greater that %value1% ) else call :Error
if %value% LSS %value2% (
echo Value is less than %value2% ) else call :Error
::Write your Command if value lie under specified range
exit

:Error
echo Value doesn't lie in the range 
::Write your command for if value doesn't lie in the range
exit
1
Set "IF=Set "or_=" & (If"
Set "OR=set "or_=1" )& (If "
Set "AND= If "
Set "THEN= set "or_=1" ) & If defined or_ "

Echo: Sample 1
%IF% 1 EQU 2 %OR% 6 NEQ 3 %OR% /I "stp" EQU "Stp" %THEN% Echo Pass

Echo: Sample 2
%IF% 1 EQU 2 %OR% 6 EQU 3 %AND% "stp" EQU "Stp" %THEN% (
    Echo Pass
) Else Echo Faill

You may mix it but should keep in mind:

  • IF ~ if (
  • OR ~ ) OR (
  • AND ~ AND (parentless)
  • THEN ~ ) Then

So example 2 will be treated as: If ( ex1 ) or ( ex2 and ex3 ) then

Dissmi
  • 11
  • 3