Conditional execution

Both OS/2 and NT offer a way to create your own one-time single-line "batch files" from the command line:

Conditional execution
Syntax Description Equivalent to
command1 & command2 Execute command2 after execution of command1 has finished command1
command2
command1 && command2 Execute command2 only if execution of command1 has finished successfully command1
IF NOT ERRORLEVEL 1 command2
command1 || command2 Execute command2 only if execution of command1 has finished unsuccessfully command1
IF ERRORLEVEL 1 command2

 

Some examples:

FORMAT A: /Q && COPY C:\DATA\*.* A:
will copy all files from C:\DATA to diskette IF and ONLY IF the format succeeds.

XCOPY C:\*.* D:\ /S 2>&1> NUL || ECHO Something terrible happened
will display your own custom error message if XCOPY fails.


page last uploaded: 23 November 2011, 19:49