亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

內部cmd.exe命令設置的ERRORLEVEL值是什么?

內部cmd.exe命令設置的ERRORLEVEL值是什么?

繁星coding 2019-07-13 15:11:41
內部cmd.exe命令設置的ERRORLEVEL值是什么?ERRORLEVEL是大多數cmd.exe命令根據一系列條件結束更改時返回的值,因此知道命令返回的值是有價值的信息,可以幫助編寫更好的批處理文件。所有外部.exe程序在結束時都會更改ERRORLEVEL(這是這兩種程序的固有機制退出過程和終末過程-32 api函數),通常這些值都有文檔記錄,但返回的值由內部cmd.exe命令在其他地方沒有完整的文檔記錄。具有部分ERRORLEVEL值的表出現在這個問題,但僅用于設置ERRORLEVEL=0的內部命令。我建議這樣的問題的OP修改它,以便也包括“不成功的命令”返回的值,但他拒絕了,并邀請我張貼我自己的問題/答案,所以,就這樣!您必須注意,與零不同的ERRORLEVEL確實是不這意味著命令失敗了!有些命令以無錯誤結尾,并返回大于零的值,以指示不同的“退出狀態”,包括內部命令(如SET /P).為了更好地利用批處理.bat文件中內置的cmd.exe命令,我們需要知道它們返回的ERRORLEVEL值以及該管理所涉及的機制。所以問題是,內部cmd.exe命令將ERRORLEVEL設置為任何價值(包括零)?
查看完整描述

3 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

在這個答案中,描述了由所有內部cmd.exe命令返回的ERRORLEVEL值;它們根據值的更改方式進行分組,并顯示為快速引用表。為了組裝這個表,我檢查了其他類似的表,但是通過在Windows8.1計算機上執行的測試來填充缺失的值。我盡了最大的努力來完整和精確地創建這些表,但是我沒有測試這里報告的每個和每個值,所以這可能是微妙的不一致。

表1-不更改先前ERRORLEVEL值的命令

BREAK
ECHO
ENDLOCAL
FOR      Not change the ERRORLEVEL by itself. See "Exit Code" below.
IF       Not change the ERRORLEVEL by itself.
PAUSE
RD       Not change the ERRORLEVEL on errors, but the "Exit Code". See below.
REM
RMDIR    Same as RD.
SET      Plain SET command (no arguments). See "Table 3" below.
TITLE

表2-根據結果將ERRORLEVEL設置為0或1的命令

Command │ Set ERRORLEVEL = 0 when       │ Set ERRORLEVEL = 1 when
────────┼───────────────────────────────┼──────────────────
CD      │Current directory was changed. │Directory not exists or is not accessible.
CHDIR   │Same as CD.                    │
COLOR   │Color was changed.             │Background and foreground colors are the same.
COPY    │File(s) was processed.         │File not found or bad parameters given.
DATE    │Date was changed or not given. │User has no admin privileges.
DEL     │Almost always, excepting when: │Bad or no parameters given.
DIR     │Same as COPY.                  │
ERASE   │Same as DEL.                   │
MD      │Directory was created.         │Directory could not be created.
MKDIR   │Same as MD.                    │
MKLINK  │Link was created.              │Link could not be created or bad parameters given.
MOVE    │File(s) was moved/renamed.     │File not found, could not be moved/renamed or bad parameters.
PUSHD   │Same as CD.                    │+ Bad switch given.
REN     │Same as MOVE.                  │
RENAME  │Same as MOVE.                  │
SETLOCAL│New environment was created.   │Bad parameters given.
TIME    │Time was changed or not given. │User has no admin privileges.
TYPE    │Same as COPY.                  │
VERIFY  │Right or no parameters given.  │Bad parameters given.
VOL     │Volume label was displayed.    │Drive not found or bad parameters given.

表3-將ERRORLEVEL設置為錯誤的命令;否則,不更改

Command      │E│ Set ERRORLEVEL to = when
─────────────┼─┼─────────────────────────────────────────
ASSOC        │*│1 = Extension associations could not be changed.
CLS          │ │1 = Bad switch given.
DPATH        │*│1 = Data path could not be established.
FTYPE        │*│1 = File type associations could not be changed.
GOTO label   │ │1 = Label not exist *in a subroutine* (equivalent to: EXIT /B 1).
KEYS         │ │1 = Bad switch given.
PATH         │*│1 = Path could not be changed.
POPD         │ │1 = Bad switch given.
PROMPT       |*│1 = Prompt could not be changed.
SET var      │*│1 = No variable with such name exists.
SET var=value│*│1 = Variable name start with "/" not enclosed in quotes.
SET /P       │*│1 = Read an empty line or at end of file.
SET /A       │*│1073750988 = Unbalanced parentheses, 1073750989 = Missing operand, 
             │ │1073750990 = Syntax error, 1073750991 = Invalid number,
             │ │1073750992 = Number larger than 32-bits, 1073750993 = Division by zero.
SHIFT        │ │1 = Bad switch given.

表3中的“E”列表示那些相應地將它們的行為更改為“擴展”狀態的命令,如相應的文檔中所描述的那樣。當啟用擴展名(默認)并將這些命令放置在以下文件中時.CMD擴展而不是.BAT第一,這些命令在結束時設置SETERRORLEVEL=0,即當表3中描述的條件是現在時。

表4-特殊情況

CALL Table1     │If the called command is anyone of Table 1 (excepting FOR and IF): set ERRORLEVEL = 0.
CALL subroutine │If the subroutine is called, not change prior ERRORLEVEL value;
                │otherwise (subroutine not exists): set ERRORLEVEL = 1.
EXIT /B, EXIT   │Not change prior ERRORLEVEL value.
EXIT /B number  │Set ERRORLEVEL to given number.
EXIT number     │Ends cmd.exe and set its returning ERRORLEVEL value to given number.
START command   │If command is started, not change ERRORLEVEL; otherwise, set ERRORLEVEL = 9059.
START /WAIT bat |When the started Batch file end, set ERRORLEVEL = value from 'EXIT number' commmand.
notExist        │If a non-existent command is entered for execution, set ERRORLEVEL = 9009.
VER             │Set ERRORLEVEL = 0 almost always. If /? parameter is given, not change ERRORLEVEL.

出口代碼管理

有兩種方法可以測試ERRORLEVEL值:VIAIF ERRORLEVEL / IF %ERRORLEVEL%命令,或使用command && thenCmd when ERRORLEVEL is 0 || elseCmd when ERRORLEVEL is not 0構造。但是,某些特定命令和重定向錯誤返回的值僅適用于第二種情況,反映在ERRORLEVEL中;我們可以將“退出代碼”稱為這個值。如果退出代碼不是零,則可以將其傳遞給執行表1的任何命令的ERRORLEVEL。elseCmd部分。有關此事的詳情,請參閱這個職位.

表5-設置退出代碼的命令或功能

Feature      │ Set Exit Code to = when
─────────────┼───────────────────────────────────────────────
command      │1 = Command not exist (when ERRORLEVEL = 9009).
redirection  │1 = File not exists in "<", path not exists or access denied in ">" ">>".
drive:       |1 = Drive unit not exists.
POPD         |1 = No matching PUSHD was previously executed.
RD           │1 = Bad switch given, 2 = Directory not found, 5 = Access denied,
             │32 = Directory in use, 145 = Directory not empty.
FOR /F       │1 = No data was processed.

例如,要測試是否發生重定向錯誤,請使用以下命令:

command > C:\Path\that\does\not\exist\file.txt || rem
if errorlevel 1 echo Previous redirection failed

在本例中,rem命令用于將退出代碼復制到ERRORLEVEL,但可以使用保留ERRORLEVEL的任何其他內部命令(例外)FORIF).

若要測試驅動器單元是否存在,請執行以下操作:

U: || rem
if errorlevel 1 echo Previous set current drive to U: unit failed

更多的例子:

rd c:\Some\directory 2> NUL || rem
if %errorlevel% equ 0 (
   echo Directory deleted
) else if %errorlevel% equ 2 (
   echo Directory not found
) else if %errorlevel% equ 5 (
   echo Can not access the directory, check rights
) else if %errorlevel% equ 32 (
   echo Can not delete current directory
) else if %errorlevel% equ 145 (
   echo Directory is not empty, use /S switch
)


(for /F "options" %%a in (input.txt) do echo %%a) || rem
if errorlevel 1 echo Previous FOR didn't processed any value


查看完整回答
反對 回復 2019-07-13
?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

我可以發誓當我檢查的時候它不在那里。也許你有過多次快速的繼承編輯。有這么多的東西需要測試,我懷疑這個答案可能需要很長時間才能完成。理想情況下,每個命令的每個選項都應該使用無效的值進行測試,不管是有還是沒有。||..我覺得有些命令產生1以外的非零值是很奇怪的,但是大多數命令會產生1。我懷疑額外的命令可能會產生0或1以外的東西。當然,還有一些額外的未列出的條件至少可以導致ERRORLEVEL 1。

查看完整回答
反對 回復 2019-07-13
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

這些表確實很有用,即使它們不包括所有現有命令的所有可能的錯誤情況(BTW是一個小示例,這是檢查管理權限的簡單方法:date %date%)。無論如何,新的數據可能會隨著發現而增加,但我真的很懷疑正常批處理文件用戶可能需要一個未在此列出的值.

查看完整回答
反對 回復 2019-07-13
  • 3 回答
  • 0 關注
  • 1347 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號