`
kanwoerzi
  • 浏览: 1644822 次
文章分类
社区版块
存档分类
最新评论

关于if errorlevel 命令

 
阅读更多
文章参考自:http://hi.baidu.com/lpf2008007/blog/item/a3fa9a3928bcd9f13a87ce8d.html
一,if errorlevel
测试前一程序的返回状态,如果该程序的返回状态大与或等于所说明的值就执行所说明的DOS命令
if errorlevel 命令让批处理文件测试一程序的返回状态值,然后根据测试结果作相应的进一步处理,当
MS-DOS遇到一条 IF errorlevel 命令时,它就将前一条MS-DOS命令的返回状态值与在IF命令中所说的值想比较,如果该程序的返回状态大于或等于IF命令所说明的值MS-DOS就执行相应的命令,如果返回状态值小于所说明的值,MS-DOS就从批处理文件中的下一条命令继续执行,如果用的是IF NOT ERRORLEVEL命令,就正好相反
例子:
下面批处理文件执行FORMATION命令,并显示基于 FORMAT的返回值的完整状态信息:
@echo off
format a:
if errorlevel 5 goto no_response
if errorlevel 4 goto error
if errorlevel 3 goto user_ctrlc
echo successful format operation
goto done
no_response
echo fixed disk will not be formatted
goto done
:error
echo error in processing.format incopplete
goto done
:user_ctrlc
echo format incomplete to ctrl_c
:done
批处理文件首先测试最大的返回状态值。记住,如果返回状态值大于 或等于有IF命令所说明的值,MS-DOS就执行所说明的命令;如果该批处理文件首先测试返回状态值是否为3,那么MS-DOS会始终执行"goto user ctrlc",而不论返回状态值是3或4,或还是5。这是因为这些返回状态值都是大于或等于3的通过将测试次序倒过来,该批处理文件就能对各返回状态值分别执行正确的那组命令
通过组合IF ERRORLEVEL和IF not ERRORLEVEL,可以测试一特定的返回状态值。例如,可以用下列命令测试返回状态值是否为3:
if errorlevel 3 if not errorlevel 4 goto user_ctrlc
注意:这时最好用if %errorlevel%==3goto user_ctrlc
二,IF NOT ERRORLEVEL
测试前一程序的返回状态,如果该程序的返回状态小于所说明的值就执行所说明的DOS命令
将not算符用于 IF ERRORLEVEL命令,批处理文件就能测试一返回值是否小于一具体值,如果是,MS-DOS就执行所说明的命令:否侧,它就从批处理文件中的 下一条命令处继续执行,IF not errorlevel 命令的 格式如下:
if not errorlevel 值 DOS命令
三, 对特定 ERRORLEVEL 值进行测试
当一个批处理文件变的 较为复杂时,可能会需要将一条 IF ERRORLEVEL命令与一条 IF NOT ERRORLEVEL m命令后侧枝从 0 至4的返回状态值
@echo off
diskcopy a: b:
if errorlevel 0 if not errorlevel 1 echo exit 0
if errorlevel 1 if not errorlevel 2 echo exit 1
if errorlevel 2 if not errorlevel 3 echo exit 2
if errorlevel 3 if not errorlevel 4 echo exit 3
if errorlevel 4 if not errorlevel 5 echo exit 4
在第一条IF命令中。第一个测试(if errorlevel 0)保证返回状态值至少为0:第二个测试(IF NOT ERRORLEVEL 1)保证返回状态值小于1.将这两个测试组合起来。该命令就保证返回状态值为0,类试的,第二条IF命令返回状态值为1,等等。
注意:当需要对特定 ERRORLEVEL 值进行测试时,最好用if %errorlevel%==值 cmmand的形式。
关于%errorlevel%的更多内容请参照《errorlevel与%errorlevel%的区别
分享到:
评论

相关推荐

    if命令IF %ERRORLEVEL% LEQ 1 goto okay

    IF [NOT] ERRORLEVEL number command IF [NOT] string1==string2 command IF [NOT] EXIST filename command NOT 指定只有条件为 false 的情况下, Windows XP 才 应该执行该命令。 ERRORLEVEL number 如果最后...

    批处理命令大全,详细介绍各种命令和语法

    exist config.sys edit config.sys 3、if errorlevel / if not errorlevel 数字 待执行的命令 如果 返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。如if errorlevel 2 goto x2 DOS程 序运行时都会...

    Dos命令大全

    3、if errorlevel / if not errorlevel 数字 待执行的命令 如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。 如if errorlevel 2 goto x2 DOS程序运行时都会返回一个数字给DOS,称为错误码...

    choice dos 批处理子命令

    然后于if errorlevel配合,根据用户的选择运行不同的命令。 注意:choice命令为DOS或者Windows系统提供的外部命令, 不同版本的choice命令语法会稍有不同,请用choice /?查看用法。 choice的命令语法(该...

    cmd操作命令和linux命令大全收集

    CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本、文件系统版本) 命令大全 1. gpedit.msc-----组策略 2. sndrec32-------录音机 3. Nslookup-------IP地址侦测器 ,是一个 监测网络中 DNS...

    超强批处理教程.rar

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、...

    DOS编程高级技巧,命令详解,相关知识

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、界面设计 二...

    如何编写批处理文件批处理文件批处理文件

    3、if errorlevel / if not errorlevel 数字 待执行的命令 如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。 如if errorlevel 2 goto x2 DOS程序运行时都会返回一个数字给DOS,称为错误码...

    CMD命令行高级教程

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF 增强的用法 第七章 DOS 编程高级技巧 一、界面设计 ...

    DOS批处理高级教程精选

    第六章 if命令讲解 第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级...

    DOS批处理高级教程精选合编

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、界面...

    经典 批处理 BAT 教程

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、界面...

    DOS批处理高级教程精选合编-整理

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、界面...

    批处理高级教程.doc

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、界面...

    批处理高级应用.doc

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、界面...

    批处理实用教程

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF增强的用法 第七章 DOS编程高级技巧 一、交互...

    dos批处理教程(实用)

    第一种用法:IF [NOT] ERRORLEVEL number command 第二种用法:IF [NOT] string1==string2 command 第三种用法:IF [NOT] EXIST filename command 第四种用法:IF 增强的用法 第七章DOS编程高级技巧 一、界面设计 二...

    cmdmute-命令行下的静音工具

    命令提示符下关闭声音的工具。 用法:cmdmute [0:1] 0-开启声音,1-关闭声音 Toggle if no argument is given. Status message printed to STDERR. Errorlevel indicates mute status.

Global site tag (gtag.js) - Google Analytics