dos中⼀些常⽤命令的errorlevel返回值⽬录
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结合errorlevel使⽤
backup
format
xcopy
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 共享冲突
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返回值内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论