批处理命令

0、功能

Performs conditional processing in batch programs.
执行批处理程序中的条件处理。

1、简介

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.
                      指定只有条件为 false 的情况下,Windows 才应该执行该命令。

  ERRORLEVEL number # Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified.
                      如果上一个DOS命令的返回值 等于或大于指定的数字number(退出代码),指定条件为 true。

  string1==string2  # Specifies a true condition if the specified text strings match. 如果指定的文字字符串匹配,指定条件为 true。

  EXIST filename    # Specifies a true condition if the specified filename exists. 如果指定的文件名存在,指定条件为 true。

  command           # Specifies the command to carry out if the condition is met.  
Command can be followed by ELSE command which will execute the command after the ELSE keyword if the specified condition is FALSE 如果符合条件,指定要执行的命令。如果指定的条件为 FALSE,命令后可跟 ELSE 命令,该命令将 在 ELSE 关键字之后执行该命令。 The ELSE clause must occur on the same line as the command after the IF. For example: ELSE 子句必须出现在同一行上的 IF 之后。例如: IF EXIST filename. ( del filename. ) ELSE ( echo filename. missing. ) The following would NOT work because the del command needs to be terminated by a newline: 由于 del 命令需要用新的一行终止,因此以下子句不会有效: IF EXIST filename. del filename. ELSE echo filename. missing Nor would the following work
, since the ELSE command must be on the same line as the end of the IF command: 由于 ELSE 命令必须与 IF 命令的尾端在同一行上,以下子句也不会有效: IF EXIST filename. del filename. ELSE echo filename. missing The following 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 会如下改变: IF [/I] string1 compare-op string2 command IF CMDEXTVERSION number command IF DEFINED variable command 其中, compare-op 可以是: 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, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed. 如果指定 /I 开关,说明要进行的字符串比较不分大小写。 /I 开关可以用于 IF 的 string1==string2 的形式上。这些比较都是通用的; 原因是,如果 string1 和 string2 都是由数字组成的,字符串会被转换成数字,进行数字比较。 The CMDEXTVERSION conditional works just 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. CMDEXTVERSION 条件的作用跟 ERRORLEVEL 的一样,除了它是在跟与命令扩展有关联的内部版本号比较。 第一个版本是 1。每次对命令扩展有相当大的增强时,版本号会增加一个。 命令扩展被停用时,CMDEXTVERSION 条件不是真的。 The DEFINED conditional works just like EXISTS except it takes an environment variable name and returns true if the environment variable is defined. 如果已定义环境变量,DEFINED 条件的作用跟 EXIST 的一样, 除了它取得一个环境变量,返回的结果是 true。 %ERRORLEVEL% will expand into a string representation of the current value of ERRORLEVEL, provided that there is not already an environment variable with the name ERRORLEVEL, in which case you will get its value instead. After running a program, the following illustrates ERRORLEVEL use: 如果没有名为 ERRORLEVEL 的环境变量,%ERRORLEVEL%会扩充为 ERROLEVEL 当前数值的字符串表达式;否则,您会得到其数值。 运行程序后,以下语句说明 ERRORLEVEL 的用法: goto answer%ERRORLEVEL% :answer0 echo Program had return code 0 :answer1 echo Program had return code 1 You can also using the numerical comparisons above: 您也可以使用以上的数字比较: IF %ERRORLEVEL% LEQ 1 goto okay %CMDCMDLINE% will expand into the original command line passed to CMD.EXE prior to any processing by CMD.EXE, provided that there is not already an environment variable with the name CMDCMDLINE, in which case you will get its value instead. 如果没有名为 CMDCMDLINE 的环境变量,%CMDCMDLINE%将在 CMD.EXE 进行任何处理前扩充为传递给 CMD.EXE 的原始命令行;否则,您会得到其数值。 %CMDEXTVERSION% will expand into a string representation of the current value of CMDEXTVERSION, provided that there is not already an environment variable with the name CMDEXTVERSION, in which case you will get its value instead. 如果没有名为 CMDEXTVERSION 的环境变量,%CMDEXTVERSION% 会扩充为 CMDEXTVERSION 当前数值的字串符表达式;否则,您会得到其数值。

2、使用举例

2.1、判断驱动器、文件或文件夹是否存在

使用句型: if exist XXX () else ()

2.1.1、判断驱动器是否存在

@echo off
if exist C: (
    echo DO_EXIST
) else (
    echo NOT_EXIST
)
pause

2.1.2、判断文件夹是否存在(1)

@echo off
if exist C:Windows (
    echo DO_EXIST
) else (
    echo NOT_EXIST
)
pause

2.1.3、判断文件夹是否存在(2)

@echo off
if exist "C:Program Files" (
    echo DO_EXIST
) else (
    echo NOT_EXIST
)
pause

Note : 文件夹的路径名包含空格时,需要用双引号括起来

2.1.4、判断文件是否存在(1)

@echo off
if exist C:myfile.txt (
    echo DO_EXIST
) else (
    echo NOT_EXIST
)
pause

2.1.5、判断文件是否存在(2)

@echo off
if exist "C:my file.txt" (
    echo DO_EXIST
) else (
    echo NOT_EXIST
)
pause

Note : 文件的路径名或者文件名中包含空格时,需要用双引号括起来

2.2、判断两个字符串是否相等

使用句型: if [/i] "string1"=="string2" () else ()

如果指定 /I 开关,说明要进行的字符串比较不分大小写。

2.2.1、字符串区分大小写比较

@echo off
if "abc"=="ABC" (
    echo DO_EQUAL
) else (
    echo NOT_EQUAL
) 
pause

2.2.2、字符串区不分大小写比较

@echo off
if /i "abc"=="ABC" (
    echo DO_EQUAL
) else (
    echo NOT_EQUAL
) 
pause

2.3、判断两个数值是否相等

使用句型: if numA compare-op numB () else ()

其中, compare-op 可以是下面中一种:

    EQU - equal 等于
    NEQ - not equal 不等于
    LSS - less than 小于
    LEQ - less than or equal 小于或等于
    GTR - greater than 大于
    GEQ - greater than or equal 大于或等于

2.3.1、应用举例

@echo off
if 1 equ 2 (
    echo DO_EQUAL
) else (
    echo NOT_EQUAL
) 
pause

2.4、判断某个变量是否已经被赋值

使用句型: if defined var () else ()

2.4.1、应用举例

@echo off
if defined windir (
    echo %windir%
) else (
    echo NOT_DEF
) 
pause

 3.使用进阶

3.1、ERRORLEVEL 的使用

如果上一个命令的返回值等于或大于指定的数字number(退出代码),指定条件为 true。

3.1.1、常用命令的返回值

以下就是几个常用命令的返回值及其代表的意义:
■ 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、拷贝过程中写盘错误 

 3.1.2、ERRORLEVEL 的应用举例

@echo off
xcopy c:myfile.txt D:
IF ERRORLEVEL 4 ECHO 拷贝过程中写盘错误
IF ERRORLEVEL 3 ECHO 预置错误阻止文件拷贝操作
IF ERRORLEVEL 2 ECHO 用户通过ctrl-c中止拷贝操作
IF ERRORLEVEL 1 ECHO 未找到拷贝文件
IF ERRORLEVEL 0 ECHO 成功拷贝文件
pause

注意: if errorlevel 的比较方式是“大于或等于”。如果返回值大于或等于指定的数字,则条件成立,运行命令。所以返回值必须按照从大到小的顺序排列。

3.2、CMDEXTVERSION 的使用

命令扩展被停用时,CMDEXTVERSION 条件不是真的

@echo off
xcopy c:myfile.txt D:
IF CMDEXTVERSION 1 ECHO 贝文件失败 
IF CMDEXTVERSION 0 ECHO 成功拷贝文件
pause
 
原文地址:https://www.cnblogs.com/LubinLew/p/WinBatchCmd_IF.html