Windows DOS(或批处理) 对命令执行结果(返回码)的判断,可使用errorlevle值来进行。例如
IF %ERRORLEVEL% GTR 0 GOTO end
表示查找jar命令,当没有找到时,则跳转到“end” 子程序继续执行。
IF %ERRORLEVEL% GTR 0 ECHO 错误代码:%errorlevel%
表示找不到jar命令时,则打印错误代码。
if %ERRORLEVEL%对数值的比较方法有如下几种:
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结合errorlevel使用
说明:环境变量errorlevel的初始值为0,当一些命令执行不成功,就会返回一个数值,如:1 ,2 等
IF-ERRORLEVEL
测试代码:
@ECHO OFF
XCOPY F:\test.bat D:\
IF ERRORLEVEL 1 ECHO 文件拷贝失败
IF ERRORLEVEL 0 ECHO 成功拷贝文件
如果文件拷贝成功,屏幕就会显示“成功拷贝文件”,否则就会显示“文件拷贝失败”。
IF ERRORLEVEL 是用来测试它的上一个DOS命令的返回值的,注意只是上一个命令的返回值,而且返回值必须依照从大到小次序顺序判断。
因此下面的批处理文件是错误的:
@ECHO OFF
XCOPY C:\AUTOEXEC.BAT D:\
IF ERRORLEVEL 0 ECHO 成功拷贝文件
IF ERRORLEVEL 1 ECHO 未找到拷贝文件
IF ERRORLEVEL 2 ECHO 用户通过ctrl-c中止拷贝操作
IF ERRORLEVEL 3 ECHO 预置错误阻止文件拷贝操作
IF ERRORLEVEL 4 ECHO 拷贝过程中写盘错误
无论拷贝是否成功,后面的:
未找到拷贝文件
用户通过ctrl-c中止拷贝操作
预置错误阻止文件拷贝操作
拷贝过程中写盘错误
都将显示出来。
以下就是几个常用命令的返回值及其代表的意义:
backup
0 备份成功
1 未找到备份文件
2 文件共享冲突阻止备份完成
3 用户用ctrl-c中止备份
4 由于致命的错误使备份操作中止
diskcomp
0 盘比较相同
1 盘比较不同
2 用户通过ctrl-c中止比较操作
3 由于致命的错误使比较操作中止
4 预置错误中止比较
diskcopy
0 盘拷贝操作成功
1 非致命盘读/写错
2 用户通过ctrl-c结束拷贝操作
3 因致命的处理错误使盘拷贝中止
4 预置错误阻止拷贝操作
format
0 格式化成功
3 用户通过ctrl-c中止格式化处理
4 因致命的处理错误使格式化中止
5 在提示“proceed with format(y/n)?”下用户键入n结束
xcopy
0 成功拷贝文件
1 未找到拷贝文件
2 用户通过ctrl-c中止拷贝操作
4 预置错误阻止文件拷贝操作
5 拷贝过程中写盘错误
if还有一个特殊的用法就是可以用来检测某个变量是否已经被定义
用法示例:
@echo off
set “str1=ok”
set “str2=no”
if defined str1 echo str1已经被定义
if defined str2 echo str2已经被定义
if defined str3 (echo str3已经被定义) else echo str3 没有被定义
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
到此这篇关于dos中一些常用命令的errorlevel返回值的文章就介绍到这了,更多相关errorlevel返回值内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论