Correct Testing Precedence of Batch File ERRORLEVELs

Αποποίηση ευθυνών για περιεχόμενο της Γνωσιακής βάσης που έχει αποσυρθεί

Αυτό το άρθρο αφορά προϊόντα για τα οποία η Microsoft δεν παρέχει πλέον υποστήριξη. Συνεπώς, το παρόν άρθρο παρέχεται "ως έχει" και δεν θα ενημερώνεται πλέον.

Summary

When you use multiple IF ERRORLEVEL statements in batch files, the order in which the ERRORLEVELs are tested numerically is important. The correct order is descending (highest to lowest). This ordering is from the way ERRORLEVELs are tested. The IF condition is set to TRUE when the ERRORLEVEL is equal to, or greater than, the ERRORLEVEL number.

More Information

The following batch file fragment demonstrates this INCORRECT behavior:

   rem (execute a program which returns an errorlevel of 0 or 1)
if errorlevel 0 goto ZERO
if errorlevel 1 goto ONE
goto END
:ZERO
echo a Zero was returned!
goto END
:ONE
echo a One was returned!
:END
The above example always branches to the "ZERO" label, regardless of success or failure. This example would work correctly if the ERRORLEVEL testing was made in descending order.


The CORRECT way to write such a batch file is as follows:
   rem (execute a program which returns an errorlevel of 0 or 1)
if errorlevel 1 goto ONE
if errorlevel 0 goto ZERO
goto END
:ZERO
echo a Zero was returned!
goto END
:ONE
echo a One was returned!
:END
Ιδιότητες

Αναγνωριστικό άρθρου: 39585 - Τελευταία αναθεώρηση: 12 Μαΐ 2003 - Αναθεώρηση: 1

Σχόλια