Conditional Commands

When an internal command or external program finishes, it returns a result called the exit code. Conditional commands allow you to perform tasks based upon the previous command's exit code. Many programs return 0 if they are successful and a non-zero value if they encounter an error.

 

AND operator &&

 

If you separate two commands by && (AND), the second command will be executed only if the first command's exit code is 0. For example, the following command will only erase files if the BACKUP operation succeeds:

 

backup c:\ a: && del c:\*.bak;*.lst

 

OR operator ||

 

If you separate two commands by || (OR), the second command will be executed only if the first command's exit code is non-zero. For example, if the following BACKUP operation fails, then ECHO will display a message:

 

backup c:\ a: || echo Error in the backup!

 

All internal commands return an exit code, but not all external programs do. Conditional commands will behave unpredictably if you use them with external programs which do not return an explicit exit code. To determine whether a particular external program returns a meaningful exit code use an ECHO %? command immediately after the program is finished. If the program's documentation does not discuss exit code, you may need to experiment with a variety of conditions to see how the exit code changes.