if (执行批处理程序中的条件处理)

小猪老师 发表于 2020-06-29 14:32
浏览次数:
在手机上阅读

if命令在批处理程序中执行条件处理。

查看英文版

目录

1 if 运行系统环境

2 if 语法

if 运行系统环境

Windows 95

Windows 98

Windows xp

Windows vista

Windows 2000

Windows 7

Windows 8

Windows 10

Windows NT

Windows ME

if 语法

Windows Vista和更高的语法

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT 指定Windows 2000或XP仅在条件为false时才执行命令。
ERRORLEVEL number 如果最后一个运行的程序返回的退出代码等于或大于指定的数字,则指定一个真条件。
string1==string2 如果指定的文本字符串匹配,则指定一个真条件。
EXIST filename 如果指定的文件名存在,则指定真实条件。
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

其中compare-op可能是以下之一:

  • EQU - 相等
  • NEQ - 不相等
  • LSS - 小于
  • LEQ - 小于或等于
  • GTR - 大于
  • GEQ - 大于或等于

/ I开关(如果指定)表示执行不区分大小写的字符串比较。/ I开关也可以用于IF的string1 == string2形式。这些比较是通用的,如果string1和string2都由所有数字组成,那么它们将被转换为数字并执行数字比较。

CMDEXTVERSION条件条件类似于ERRORLEVEL,但它与与命令扩展关联的内部版本号进行比较。第一个版本是1。当对命令扩展进行了重大增强时,它将增加一个。禁用命令扩展时,CMDEXTVERSION条件永远不会为真。

DEFINED条件条件的工作方式类似于EXISTS,不同之处在于它使用环境变量名称,并且如果定义了环境变量,则返回true 。

如果没有名称为ERRORLEVEL的环境变量,则%ERRORLEVEL%会扩展为ERRORLEVEL当前值的字符串,在这种情况下,您可以获取它的值。运行程序后,以下示例说明了ERRORLEVEL的用法:

goto answer%ERRORLEVEL%
:answer0
echo Program had return code 0
:answer1
echo Program had return code 1

您还可以使用以下数值比较:

IF %ERRORLEVEL% LEQ 1 goto okay

如果没有CMDCMDLINE环境变量,则%CMDCMDLINE%会扩展到在CMD.EXE进行任何处理之前传递给CMD.EXE的命令行。在这种情况下,您可以获取它的值。

如果没有名称为CMDEXTVERSION的环境变量,则%CMDEXTVERSION%会扩展为CMDEXTVERSION值的字符串,在这种情况下,您可以获取其值。

Windows XP和更早的语法

在批处理程序中执行条件处理。

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT 指定Windows仅在条件为false时才执行命令。
ERRORLEVEL number 如果最后一个运行的程序返回的退出代码等于或大于指定的数字,则指定一个真条件。
command 指定满足条件时要执行的命令。
string1==string2 如果指定的文本字符串匹配,则指定一个真条件。
EXIST filename 如果指定的文件名存在,则指定真实条件。

Windows Vista and later syntax

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT Specifies that Windows 2000 or XP should carry out the command only if the condition is false.
ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.
string1==string2 Specifies a true condition if the specified text strings match.
EXIST filename Specifies a true condition if the specified filename exists.
command Specifies the command to carry out if the condition is met. Command can be followed by the ELSE command that will execute the command after the ELSE keyword if the specified condition is FALSE.

The ELSE clause must occur on the same line as the command after the IF. For example:

IF EXIST filename. (
 del filename.
) ELSE (
 echo filename. missing.
)

The example below would NOT work because the del command needs to be terminated by a newline:

IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the example below work, because the ELSE command must be on the same line as the end of the IF command:

IF EXIST filename. del filename.
ELSE echo filename. missing

The example below would work if you want it all on one line:

IF EXIST filename. (del filename.) ELSE echo filename. missing

If Command Extensions are enabled IF changes as follows:

IF [/I] string1 compare-op string2 command
IF CMDEXTVERSION number command
IF DEFINED variable command

where compare-op may be one of:

  • EQU - equal
  • NEQ - not equal
  • LSS - less than
  • LEQ - less than or equal
  • GTR - greater than
  • GEQ - greater than or equal

and the /I switch, if specified, says to do case insensitive string compares. The /I switch can also be used on the string1==string2 form of IF. These comparisons are generic, if both string1 and string2 are composed of all numeric digits, then they're converted to numbers and a numeric comparison is performed.

The CMDEXTVERSION conditional works like ERRORLEVEL, except it is comparing against an internal version number associated with the Command Extensions. The first version is 1. It will be incremented by one when significant enhancements are added to the Command Extensions. CMDEXTVERSION conditional is never true when Command Extensions are disabled.

The DEFINED conditional works like EXISTS except it takes an environment variable name and returns true if the environment variable is defined.

%ERRORLEVEL% expands into a string of the current value of ERRORLEVEL, provided there is not already an environment variable with the name ERRORLEVEL, in which case you get its value. After running a program, the example below illustrates ERRORLEVEL use:

goto answer%ERRORLEVEL%
:answer0
echo Program had return code 0
:answer1
echo Program had return code 1

You can also use the numerical comparisons below:

IF %ERRORLEVEL% LEQ 1 goto okay

%CMDCMDLINE% expands to the command line passed to CMD.EXE before any processing by CMD.EXE, provided there is not a CMDCMDLINE environment variable, in which case you get its value.

%CMDEXTVERSION% expands into a string of the value of CMDEXTVERSION, provided there is not already an environment variable with the name CMDEXTVERSION, in which case you get its value.

Windows XP and earlier syntax

Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT Specifies that Windows should carry out the command only if the condition is false.
ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.
command Specifies the command to carry out if the condition is met.
string1==string2 Specifies a true condition if the specified text strings match.
EXIST filename Specifies a true condition if the specified filename exists.

查看英文版

查看中文版

其他命令行

ipconfig | icacls | ifshlp.sys |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条