目錄
- ATTRIB.EXE
- EXTRACT.EXE
- FC.EXE
- FIND.EXE
- FORMAT.COM
- FTP.EXE
- MORE.COM
- MOVE.EXE
- PING.EXE
- SORT.EXE
- START.EXE
- SUBST.EXE
- TRACERT.EXE
- XCOPY.EXE
- if結(jié)合errorlevel使用
- backup
- format
- xcopy
Windows DOS(或批處理) 對(duì)命令執(zhí)行結(jié)果(返回碼)的判斷,可使用errorlevle值來(lái)進(jìn)行。例如
IF %ERRORLEVEL% GTR 0 GOTO end
表示查找jar命令,當(dāng)沒(méi)有找到時(shí),則跳轉(zhuǎn)到“end” 子程序繼續(xù)執(zhí)行。
IF %ERRORLEVEL% GTR 0 ECHO 錯(cuò)誤代碼:%errorlevel%
表示找不到j(luò)ar命令時(shí),則打印錯(cuò)誤代碼。
if %ERRORLEVEL%對(duì)數(shù)值的比較方法有如下幾種:
EQU - 等于
NEQ - 不等于
LSS - 小于
LEQ - 小于或等于
GTR - 大于
GEQ - 大于或等于
ATTRIB.EXE
(a) Target file/folder not found = ERRORLEVEL 1
(b) Invalid switch = ERRORLEVEL 1
(c) Sharing violation (target file in use) = ERRORLEVEL 1
(d) Invalid drive specification = ERRORLEVEL 1
(e) Drive not ready (either Abort or Fail reply) = ERRORLEVEL 1
(f) Invalid/wrong number of parameters = ERRORLEVEL 1
EXTRACT.EXE
(a) Invalid switch = ERRORLEVEL 1
FC.EXE
(a) Insufficient number of filespecs = ERRORLEVEL 1
(b) Too many filenames on command line = ERRORLEVEL 1
(c) Sharing Violation + Abort reply = ERRORLEVEL 5
(Note: Fail reply returns ERRORLEVEL 0)
(d) Drive not ready = ERRORLEVEL 18
(Note: Fail reply returns ERRORLEVEL 0)
FIND.EXE
(a) Target string found (=f0und) = ERRORLEVEL 0
(b) Target string missing (=m1ssing) = ERRORLEVEL 1
(c) find /? = ERRORLEVEL 1
(d) Parameter format not correct = ERRORLEVEL 2
(e) Specified file to search not found = ERRORLEVEL 2
(f) Specified file in use + Fail reply = ERRORLEVEL 2
(g) Drive not ready + Fail reply = ERRORLEVEL 2
(h) Specified file in use + Abort reply = ERRORLEVEL 5
(i) Drive not ready + Abort reply = ERRORLEVEL 5
FORMAT.COM
(a) Drive not ready = ERRORLEVEL 4
(There is no Abort, Retry, Fail stall)
FTP.EXE
(a) Brief help (use: ftp -h for Brief help) = ERRORLEVEL 2
(b) Error opening script file (file missing) = ERRORLEVEL 2
(c) Invalid switch = ERRORLEVEL 2
MORE.COM
(a) Invalid switch (MORE doesn't accept switches) = ERRORLEVEL 1
(b) Drive not ready (Abort reply) = ERRORLELEL 5
(Note: Fail reply returns ERRORLEVEL 0)
MOVE.EXE
(a) Required parameter missing = ERRORLEVEL 1
(b) Unable to create destination = ERRORLEVEL 1
(c) Unable to open source = ERRORLEVEL 1
(you see this when trying to MOVE a folder from one drive
to another. You need to use XCOPY /S followed by DELTREE,
since MOVE won't handle folder moves across drives)
(d) Sharing violation + Fail reply = ERRORLEVEL 1
(Note: file is nevertheless COPIED, not moved, in this case)
(e) Sharing violation + Abort reply = ERRORLEVEL 5
(Note: file is nevertheless COPIED, not moved, in this case)
(f) Drive not ready (Abort reply) = ERRORLEVEL 18
(Note: Fail reply - unusually - returns ERRORLEVEL 1)
PING.EXE
(a) Unknown host = ERRORLEVEL 1
(usually=name not found on DomainNameServer)
(b) Brief help (with no parameter) = ERRORLEVEL 1
Note: for PING Brief help with /? switch, ERRORLEVEL is 0
(c) Invalid switch = ERRORLEVEL 1
(and displays the Brief help as well)
(d) Interrupted with [Ctrl-C] = ERRORLEVEL 255
SORT.EXE
(a) Invalid switch = ERRORLEVEL 1
(b) Drive not ready (Abort reply) = ERRORLEVEL 15
(Note: Fail reply returns ERRORLEVEL 0)
START.EXE
(a) start /? (real mode) = ERRORLEVEL 1
(b) start /? (GUI) = ERRORLEVEL 255
(c) Can't find file specified for START = ERRORLEVEL 255
(d) No file association for specified file = ERRORLEVEL 255
(e) Drive not ready = ERRORLEVEL 255
(There is no Abort, Retry, Fail stall)
SUBST.EXE
(a) Invalid parameter = ERRORLEVEL 1
(b) Invalid switch = ERRORLEVEL 1
(c) Path not found = ERRORLEVEL 1
(d) Drive not ready (Abort reply) = ERRORLEVEL 21
(Note: Fail reply - unusually - returns ERRORLEVEL 1)
TRACERT.EXE
(a) Unable to resolve target system name = ERRORLEVEL 1
(usually=name not found on DomainNameServer)
(b) Invalid switch = ERRORLEVEL 1
(c) Brief help (no parameter) = ERRORLEVEL 1
(d) Interrupted with [Ctrl-C] = ERRORLEVEL 255
Note: for TRACERT Brief help, type command without parameters
XCOPY.EXE
(a) File not found = ERRORLEVEL 1
(b) Invalid date in /d switch = ERRORLEVEL 4
(c) Invalid number of parameters = ERRORLEVEL 4
(d) Invalid parameter = ERRORLEVEL 4
(e) Device not ready = ERRORLEVEL 4
(f) Unable to create directory = ERRORLEVEL 4
(g) System can't find file = ERRORLEVEL 5
(this occurs when device such as NUL is used as Source file)
if結(jié)合errorlevel使用
說(shuō)明:環(huán)境變量errorlevel的初始值為0,當(dāng)一些命令執(zhí)行不成功,就會(huì)返回一個(gè)數(shù)值,如:1 ,2 等
IF-ERRORLEVEL
測(cè)試代碼:
@ECHO OFF
XCOPY F:\test.bat D:\
IF ERRORLEVEL 1 ECHO 文件拷貝失敗
IF ERRORLEVEL 0 ECHO 成功拷貝文件
如果文件拷貝成功,屏幕就會(huì)顯示“成功拷貝文件”,否則就會(huì)顯示“文件拷貝失敗”。
IF ERRORLEVEL 是用來(lái)測(cè)試它的上一個(gè)DOS命令的返回值的,注意只是上一個(gè)命令的返回值,而且返回值必須依照從大到小次序順序判斷。
因此下面的批處理文件是錯(cuò)誤的:
@ECHO OFF
XCOPY C:\AUTOEXEC.BAT D:\
IF ERRORLEVEL 0 ECHO 成功拷貝文件
IF ERRORLEVEL 1 ECHO 未找到拷貝文件
IF ERRORLEVEL 2 ECHO 用戶(hù)通過(guò)ctrl-c中止拷貝操作
IF ERRORLEVEL 3 ECHO 預(yù)置錯(cuò)誤阻止文件拷貝操作
IF ERRORLEVEL 4 ECHO 拷貝過(guò)程中寫(xiě)盤(pán)錯(cuò)誤
無(wú)論拷貝是否成功,后面的:
未找到拷貝文件
用戶(hù)通過(guò)ctrl-c中止拷貝操作
預(yù)置錯(cuò)誤阻止文件拷貝操作
拷貝過(guò)程中寫(xiě)盤(pán)錯(cuò)誤
都將顯示出來(lái)。
以下就是幾個(gè)常用命令的返回值及其代表的意義:
backup
0 備份成功
1 未找到備份文件
2 文件共享沖突阻止備份完成
3 用戶(hù)用ctrl-c中止備份
4 由于致命的錯(cuò)誤使備份操作中止
diskcomp
0 盤(pán)比較相同
1 盤(pán)比較不同
2 用戶(hù)通過(guò)ctrl-c中止比較操作
3 由于致命的錯(cuò)誤使比較操作中止
4 預(yù)置錯(cuò)誤中止比較
diskcopy
0 盤(pán)拷貝操作成功
1 非致命盤(pán)讀/寫(xiě)錯(cuò)
2 用戶(hù)通過(guò)ctrl-c結(jié)束拷貝操作
3 因致命的處理錯(cuò)誤使盤(pán)拷貝中止
4 預(yù)置錯(cuò)誤阻止拷貝操作
format
0 格式化成功
3 用戶(hù)通過(guò)ctrl-c中止格式化處理
4 因致命的處理錯(cuò)誤使格式化中止
5 在提示“proceed with format(y/n)?”下用戶(hù)鍵入n結(jié)束
xcopy
0 成功拷貝文件
1 未找到拷貝文件
2 用戶(hù)通過(guò)ctrl-c中止拷貝操作
4 預(yù)置錯(cuò)誤阻止文件拷貝操作
5 拷貝過(guò)程中寫(xiě)盤(pán)錯(cuò)誤
if還有一個(gè)特殊的用法就是可以用來(lái)檢測(cè)某個(gè)變量是否已經(jīng)被定義
用法示例:
@echo off
set “str1=ok”
set “str2=no”
if defined str1 echo str1已經(jīng)被定義
if defined str2 echo str2已經(jīng)被定義
if defined str3 (echo str3已經(jīng)被定義) else echo str3 沒(méi)有被定義
pause>nul
if errorlevel 0
===> 表示errorlevel 等于或大于0
if not errorlevel 0
===> 表示errorlevel 小于0
if %errorlevel% == 0 或者 if “%errorlevel%” == “0 ” 或 if “%errorlevel%” equ “0 ”
===> 表示errorlevel 等于0
if %errorlevel% neq 0 或者 if “%errorlevel%” neq “0 ” 或 if “%errorlevel%” neq “0 ”
===> 表示errorlevel 不等于0
到此這篇關(guān)于dos中一些常用命令的errorlevel返回值的文章就介紹到這了,更多相關(guān)errorlevel返回值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- cmd if條件 條件判斷
- dos if 條件判斷
- bat批處理 if 命令示例詳解
- bat If 命令常用判斷介紹
- bat中errorlevel與%errorlevel%的區(qū)別
- IF-ERRORLEVEL使用方法