How can I use several conditions in the "If" in a batch file?

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

If it’s not so difficult, I can at least use something like:

 if value1 < value < value2 

Basically, my goal is to check if the current system time falls over a certain interval (more precisely, 2.05 AM and 7.55 AM), and if this happens, to execute certain commands.

+4
source share
3 answers

By adding dbenham to the answer, you can emulate both logical operators (AND, OR) using a combination of if and goto .

To check condition 1 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 check condition 1 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 

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

+13
source

There are no logical operators in the batch. But AND is easy to imitate two IF statements

 if value1 lss value if value lss value2 REM do something 

Batch IF statements do not know how to compare times. The IF package knows how to compare integers and strings.

One option is to convert the time to minutes or seconds after midnight.

Another option is to format the time with hours, minutes (and seconds, if necessary), up to two digits in width (0 if necessary). The clock should be in a 24-hour format (wartime).

+5
source

You can split your state into 2 and use if and if to set 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 
0
source

Source: https://habr.com/ru/post/1415785/


All Articles