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 的一样,除了它是在跟与命令扩展有关联的内部版本号比较。
第一个版本是 。每次对命令扩展有相当大的增强时,版本号会增加一个。
命令扩展被停用时,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 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
 

批处理命令 - if的更多相关文章

  1. 批处理命令——call 和 start

    一.call命令总结 [1]call命令简介 学过汇编或C的朋友,肯定都知道call指令表示什么意思.其实,在这里它的意思也是一样的.在批处理脚本中,call命令用来从一个批处理脚本中调用另一个批处理 ...

  2. 批处理命令 BAT备份MySQL数据库

    批处理命令 BAT备份MySQL数据库 作者: 字体:[增加 减小] 类型:转载 时间:2009-07-23我要评论 MySQL数据的备份工具也许有很多,在这我要给大家分享一下通过DOS批处理命令和M ...

  3. SQL Server数据库备份:通过Windows批处理命令执行

    通过Windows批处理命令执行SQL Server数据库备份 建立mybackup.bat ,输入以下内容直接运行该脚本,即可开始自动备份数据库也可把该脚本加入windows任务计划里执行. --- ...

  4. TFS 自动同步Server 端文件的批处理命令

    TFS 自动同步Server 端文件的批处理命令 目前在我们组的工作中很多时候需要将TFS上Server端的代码自动无人值守的同步到本地中来, 找到了一些解决方案的资料http://bbs.scmro ...

  5. 批处理命令——goto 和 :

    谈起goto,相信大家应该想到的是面向过程编程.其实,这就相当于当有人向你谈起class,意味着你就懂得面向对象编程.如果你不懂,那么你们的沟通将会很困难.不懂我说的啥意思吗?请参见曾经分享王路的一篇 ...

  6. TortoiseSVN常用批处理命令 分类: C# 2014-08-09 11:31 648人阅读 评论(1) 收藏

    TortoiseSVN作为源代码管理软件,估计用过的都会说好,在Windows下,配合批处理命令,往往可以事半功倍,整理了下常用的批处理命令: (将下面的内容修改后,保存为*.bat文件执行即可) : ...

  7. C++程序中调用MPI并行的批处理命令

    问题来源:在使用MPI时,将程序并行实现了,运行时需要在dos窗口下输入批处理命令,以完成程序的执行. 如:mpiexec -localroot -n 6 d:/mpi/pro.exe 但每次这样挺麻 ...

  8. BATCH(BAT批处理命令语法)

    bat语法备忘扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的文件就是批处理文件[@more@] bat语法备忘扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的 ...

  9. C# 动态执行批处理命令

    本文转载:http://www.cnblogs.com/lenic/p/4097045.html C# 动态执行一系列控制台命令,并允许实时显示出来执行结果时,可以使用下面的函数.可以达到的效果为: ...

  10. DOS批处理命令-if语句

    IF语句是批处理中执行的条件分歧处理. 批处理中,IF分歧的写法有好几种,接下来,我们来一个一个的分析IF语法的结构. 1.IF [NOT] ERRORLEVEL 番号 批处理命令 当ERRORLEV ...

随机推荐

  1. 解决sharepoint 2010 用户配置文件同步服务 正在启动

    用户配置文件同步服务一直显示“正在启动”,而且无法停止,如下办法可以停止这个服务: 在sharepoint power shell 中执行下面的命令: Get-spserviceinstance 获取 ...

  2. 1.总结---tr()和QTextCodec对象

    1. 关于Qt 中的tr()函数-------http://tscsh.blog.163.com/blog/static/200320103201310213312518/ 在论坛中漂,经常遇到有人遇 ...

  3. 用Sqlplus手动创建Oracle11g数据库

    用Sqlplus手动创建Oracle数据库 刚开始学习Oracle数据库,菜鸟一个,使用sqlplus创建数据库遇到了很多问题,通过不断地百度,终于创建成功了.所以顺便把整个过程中犯的一些最低级的错误 ...

  4. Java通过反射机制修改类中的私有属性的值

    首先创建一个类包含一个私有属性: class PrivateField{ private String username = "Jason"; } 通过反射机制修改username ...

  5. Teamwork——Week4 团队项目之NABC

    项目框架——NABC模型 一.N(Need需求) 我们组主要的用户对象是第三小组——UI小组的同学们,因此我们的用户需求就是他们的数据需求. 1)提供给UI小组整理好的数据库,和前一组讨论好数据结构. ...

  6. c语言编程之二叉排序树

    二叉排序树,又称为二叉查找树.它是一颗空树,或者是具有下面的性质的二叉树: 1.若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值: 2.若它的右子树不空,则右子树上所有节点的值均大于它的根 ...

  7. Android基础整理之四大组件Activity

    最近准备系统的重新整理复习一下Android的各方面的知识,本着知识分享的原则,我就把梳理过程中一些东西给记录下来,权当一个学习笔记吧. 下面步入正题..... 什么是Activity Activit ...

  8. 基于word制作网站webhelp

    处理问题描述:现在我有个javaweb项目,需要在portal上面点击help即可打开: 当前搜索百度(谷歌不能用了),没有找到更好的解决方案,自己想了个比较简单实用的方法,仅供参考: 设计原理:利用 ...

  9. 自定义异常时如何定义checked异常和unchecked异常

    When defining your own exception type, study the existing exception classes in the Java API and try ...

  10. [转]Eclipse遇到的常见问题

    1.  提示:“Setting build path” has encountered a problem,Could not write file D:\\workspace\demo\.class ...