Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmd.exe: complex conditions?

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.

like image 574
shoosh Avatar asked May 05 '10 10:05

shoosh


People also ask

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

What is cmd.exe used for?

The command processor component in the Windows operating system that accepts and executes instructions on a command line. While providing more flexibility, CMD. EXE supports most of the operations that were previously handled with the COMMAND.COM processor in DOS.

What does cmd.exe K mean?

If it's an internal cmd command or a batch file, then the command processor is run with the /K switch to cmd.exe. The /K switch keeps the window open after the command is run.

Is cmd.exe a shell?

Windows Command Prompt (also known as the command line, cmd.exe or simply cmd) is a command shell based on the MS-DOS operating system from the 1980s that enables a user to interact directly with the operating system.


1 Answers

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 such 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.

like image 104
Joey Avatar answered Sep 25 '22 03:09

Joey