批處理文件中的errorlevel用法:
一、IF-ERRORLEVEL
建立report.bat,內容如下:
@echo off
sqlplus %2/%3@%1@report.sql
::errorlevel indicate whether
sql script is processed successfully.
if errorlevel 4 echo REPORTING[ERR]: Error 4 occurs
& goto exit
if errorlevel 3 echo REPORTING[ERR]: Error 3 occurs
& goto exit
if errorlevel 2 echo REPORTING[ERR]: Error 2
occurs & goto exit
if errorlevel 1 echo REPORTING[ERR]: Error 1 occurs
& goto exit
if errorlevel 0 echo REPORTING[INFO]: Reports have
been generated successfully & goto exit
:exit
很多DOS程序在運行結束後會返回一個數字值用來表示程序運行的結果(或者狀態),通過if errorlevel命令可以判斷程序的返回值,根據不同的返回值來決定執行不同的命令
注意:if errorlevel 的比較方式是“大於或等於”。如果返回值大於或等於指定的數字,則條件成立,運行命令。所以返回值必須按照從大到小的順序排列。