if语句
判断俩个字符串是否相等
if "string1" == "string2" command 语句
例子:
1 | @echo off |
判断俩个数值是否相等
if 数值1 equ 数值2 command 语句
其他关系符号:
中文含义 | 关系符 | 英文解释 |
---|---|---|
等于 | equ | equal |
大于 | gtr | greater than |
大于或等于 | geq | greater than or equal |
小于 | lss | less than |
小于或等于 | leq | less than or equal |
不等于 | neq | no equal |
例子:
1 | set /p num1=请输入数字: |
判断驱动器是否存在
if [not] exist filename command
例子:
1 | if exist "e:" (echo e盘存在) else echo e盘不存在 |
判断变量是否定义
if defined 变量 command 语句
例子:
1 | if defined a (echo 变量a已定义) else echo 变量a没被定义 |
判断命令返回值
if errorlevel语句特点:
if errorlevel 0 cmmand
, 含义是:如果返回的错误码值大于或等于0的时,将执行cmmand操作if %errorlevel%==0 cmmand
,含义是:如果返回的错误码值等于0的时候,将执行cmmand操作
例子:
1 | net user |
if not语句
- 否的意思, 失败的时候先执行
- if not “字符串1”==”字符串2” command 语句;
- if not数值1 equ 数值2 command 语句;
- if not exist filename command 语句;
- if not defined 变量 command 语句;
- if not errorlevel 数值 command 语句。