在批处理程序中执行条件处理。
Syntax
if [not] ERRORLEVEL <number> <command> [else <expression>]
if [not] <string1>==<string2> <command> [else <expression>]
if [not] exist <filename> <command> [else <expression>]
如果启用了命令扩展,请使用以下语法:
if [/i] <string1> <compareop> <string2> <command> [else <expression>]
if cmdextversion <number> <command> [else <expression>]
if defined <variable> <command> [else <expression>]
Parameters
Parameter | Description |
---|---|
not | 指定仅当条件为 false 时,才应执行该命令。 |
错误级别 <number> |
Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than number. |
<command> |
指定满足上述条件时应执行的命令。 |
<string1>==<string2> |
Specifies a true condition only if string1 and string2 are the same. 这些值可以是文本字符串或批处理变量(例如,%1 )。 无需将文本字符串括在引号中。 |
存在 <filename> |
如果指定的文件名存在,则指定 true 条件。 |
<compareop> |
指定三个字母比较运算符,包括:
|
/i | 强制字符串比较忽略大小写。 You can use /i on the string1==string2 form of if. These comparisons are generic, in that if both string1 and string2 are comprised of numeric digits only, the strings are converted to numbers and a numeric comparison is performed. |
cmdextversion <number> |
仅当与 Cmd.exe 命令扩展功能关联的内部版本号等于或大于指定的数字时,才指定一个 true 条件。 第一个版本为 1。 当向命令扩展添加重要增强功能时,它会增加一个增量。 The cmdextversion conditional is never true when command extensions are disabled (by default, command extensions are enabled). |
定义的 <variable> |
Specifies a true condition if variable is defined. |
<expression> |
Specifies a command-line command and any parameters to be passed to the command in an else clause. |
/? | 在命令提示符下显示帮助。 |
Remarks
If the condition specified in an if clause is true, the command that follows the condition is carried out. If the condition is false, the command in the if clause is ignored and the command executes any command that is specified in the else clause.
当程序停止时,它将返回退出代码。 To use exit codes as conditions, use the errorlevel parameter.
If you use defined, the following three variables are added to the environment: %errorlevel%, %cmdcmdline%, and %cmdextversion%.
%errorlevel%: Expands into a string representation of the current value of the ERRORLEVEL environment variable. 此变量假定还没有名为 ERRORLEVEL 的现有环境变量。 如果有,将改为获取 ERRORLEVEL 值。
%cmdcmdline%: Expands into the original command line that was passed to Cmd.exe prior to any processing by Cmd.exe. 这假定还没有名为 CMDCMDLINE 的现有环境变量。 如果有,将改为获取该 CMDCMDLINE 值。
%cmdextversion%: Expands into the string representation of the current value of cmdextversion. 这假定还没有名为 CMDEXTVERSION 的现有环境变量。 如果有,将改为获取该 CMDEXTVERSION 值。
You must use the else clause on the same line as the command after the if.
Examples
若要显示消息 找不到数据文件Product.dat,请键入:
if not exist product.dat echo Cannot find data file
若要在驱动器 A 中格式化磁盘,并在格式设置过程中发生错误时显示错误消息,请在批处理文件中键入以下行:
:begin
@echo off
format a: /s
if not errorlevel 1 goto end
echo An error occurred during formatting.
:end
echo End of batch program.
若要从当前目录中删除文件Product.dat,或者在找不到Product.dat时显示消息,请在批处理文件中键入以下行:
IF EXIST Product.dat (
del Product.dat
) ELSE (
echo The Product.dat file is missing.
)
Note
这些行可以组合成一行,如下所示:
IF EXIST Product.dat (del Product.dat) ELSE (echo The Product.dat file is missing.)
若要在运行批处理文件后回显 ERRORLEVEL 环境变量的值,请在批处理文件中键入以下行:
goto answer%errorlevel%
:answer1
echo The program returned error level 1
goto end
:answer0
echo The program returned error level 0
goto end
:end
echo Done!
若要转到正常标签(如果 ERRORLEVEL 环境变量的值小于或等于 1,请键入:
if %errorlevel% LEQ 1 goto okay