Run command2 only if command1 succeeded in cmd windows shell

user674669 picture user674669 · Sep 13, 2011 · Viewed 31.3k times · Source

How do we combine commands in cmd shell language such that the second command is only executed if the first command successfully completed?

something like following bash-command

make && ./a.out

a.out is only executed if make was successful

Answer

aioobe picture aioobe · Sep 13, 2011

The following

command1 && command2

should work on cmd as well. Quote from here:

When using cmd.exe, you can put multiple commands on the same line by using ‘&’ or ‘&&’ between commands. Using a single ampersand (&) will cause the first command and then the second command to be run in sequence. Using double ampersands (&&) introduces error checking. The second command will run only if the first command is successful.