IF-ERRORLEVEL

IF-ERRORLEVEL

建立TEST4.BAT,内容如下:
@ECHO OFF
XCOPY C:/AUTOEXEC.BAT D:/
IF ERRORLEVEL 1 ECHO 文件拷贝失败
IF ERRORLEVEL 0 ECHO 成功拷贝文件
然后执行文件:
C:/>TEST4
如果文件拷贝成功,屏幕就会显示“成功拷贝文件”,否则就会显示“文件拷贝失败”。
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 拷贝过程中写盘错误
详细出处参考:http://www.jb51.net/article/7525.htm

 

 

ERRORLEVEL is not %ERRORLEVEL%
The command interpreter cmd.exe has a concept known as the error level, which is the exit code of the program most recently run. You can test the error level with the IF ERRORLEVEL command:

IF ERRORLEVEL 1 ECHO error level is 1 or more
<sidebar>
The IF ERRORLEVEL n test succeeds if the error level is n or more. This was presumably because there were programs that expressed different degrees of failure with higher and higher exit codes. For example, the diff program has three exit codes: 0 means the files are the same; 1 means the files are different; 2 means that something terrible happened. There are also programs that use an exit code of zero to mean success and anything else to mean failure.
</sidebar>

In addition to this internal state, you can, if you wish, create an environment variable with the name ERRORLEVEL, in the same way that you can create an environment variable called FRED. But, as with FRED, that variable won't have any effect on the error level.

rem this next command sets the error level to zero
CMD /C EXIT 0
set ERRORLEVEL=1
if ERRORLEVEL 1 echo Does this print?
The message is not printed because the ERRORLEVEL environment variable has no effect on the error level. It's just a variable whose name happens to coincide with a command processor concept.

set BANKBALANCE=$1,000,000.00
"Hey, when I tried to withdraw the money, I got an insufficient funds error. What am I doing wrong?"

Now, it does happen to be the case that if command extensions are enabled and you say %ERRORLEVEL%, then the command processor first looks for an environment variable called ERRORLEVEL, and if it can't find one, then it replaces %ERRORLEVEL% with the current value of the internal error level value. It's a fallback step, in the same way that your neighbor is a fallback delivery location if you aren't home. If you file a change-of-address form for yourself, that doesn't affect packages sent to your neighbor.

The same behavior can be seen with %CD%: If you did not explicitly set an environment variable called CD, then %CD% expands to the command processor's current directory. But you can't change directories by saying set CD=C:/Windows.

I can think of a few reasons why this feature may have been added.

So you can include the error level in a log file:
ECHO error level is %ERRORLEVEL%>logfile
So you can perform other types of tests against the error level, for example, to perform an equality test:
IF %ERRORLEVEL% EQU 1 echo Different!
But I'm digressing. My point for today is that the error level is not the same as the ERRORLEVEL environment variable.


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lijin912/archive/2009/11/27/4885864.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
执行批处理程序中的条件处理。 IF [NOT] ERRORLEVEL number command IF [NOT] string1==string2 command IF [NOT] EXIST filename command NOT 指定只有条件为 false 的情况下, Windows XP 才 应该执行该命令。 ERRORLEVEL number 如果最后运行的程序返回一个等于或大于 指定数字的退出编码,指定条件为 true。 string1==string2 如果指定的文字字符串匹配,指定条件为 true。 EXIST filename 如果指定的文件名存在,指定条件为 true。 command 如果符合条件,指定要执行的命令。如果指定的 条件为 FALSE,命令后可跟一个执行 ELSE 关键字后的命令的 ELSE 命令。 ELSE 子句必须在 IF 之后出现在同一行上。例如: IF EXIST filename. ( del filename. ) ELSE ( echo filename. missing. ) 因为 del 命令需要用一个新行终止,以下子句不会有效: IF EXIST filename. del filename. ELSE echo filename. missing 由于 ELSE 命令必须与 IF 命令的尾端在同一行上,以下子句也 不会有效: IF EXIST filename. del filename. ELSE echo filename. missing 如果都放在同一行上,以下子句有效: IF EXIST filename. (del filename.) ELSE echo filename. missing 如果命令扩展名被启用,IF 会如下改变: IF [/I] string1 compare-op string2 command IF CMDEXTVERSION number command IF DEFINED variable command 其中,比较运算符可以是: EQU - 等于 NEQ - 不等于 LSS - 小于 LEQ - 小于或等于 GTR - 大于 GEQ - 大于或等于 及 /I 开关;如果该开关被指定,则说明要进行的字符串比较不分 大小写。/I 开关可以用于 IF 的 string1==string2 的形式上。这些 比较都是通用的;原因是,如果 string1 和 string2 都是由数字 组成的,字符串会被转换成数字,进行数字比较。 CMDEXTVERSION 条件的作用跟 ERRORLEVEL 的一样,除了它 是在跟与命令扩展名有关联的内部版本号比较。第一个版本 是 1。每次对命令扩展名有相当大的增强时,版本号会增加一个。 命令扩展名被停用时,CMDEXTVERSION 条件不是真的。 如果已定义环境变量,DEFINED 条件的作用跟 EXISTS 的一样, 除了它取得一个环境变量,返回的结果是 true。 如果没有名为 ERRORLEVEL 的环境变量,%ERRORLEVEL% 会扩充为 ERROLEVEL 当前数值的字符串表达式;否则,您会得到 其数值。运行程序后,以下语句说明 ERRORLEVEL 的用法: goto answer%ERRORLEVEL% :answer0 echo Program had return code 0 :answer1 echo Program had return code 1 您也可以使用以上的数字比较: 如果没有名为 CMDCMDLINE 的环境变量,%CMDCMDLINE% 将在 CMD.EXE 进行任何处理前扩充为传递给 CMD.EXE 的原始 命令行;否则,您会得到其数值。 如果没有名为 CMDEXTVERSION 的环境变量, %CMDEXTVERSION% 会扩充为 CMDEXTVERSION 当前数值的 字串符表达式;否则,您会得到其数值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值