六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 29|回复: 0

if 语句详解

[复制链接]

升级  42%

5

主题

5

主题

5

主题

童生

Rank: 1

积分
21
 楼主| 发表于 2013-2-7 03:39:40 | 显示全部楼层 |阅读模式
if语句一共有3种模式,如下:

  • IF [NOT] string1==string2 command (else command)
  • IF [NOT] EXIST filename command (else command)
  • IF [NOT] ERRORLEVEL number command (else command)
NOT   指定只有条件为 false 的情况下, Windows XP 才应该执行该命令。
ERRORLEVEL number 如果最后运行的程序返回一个等于或大于指定数字的退出编码,指定条件为 true

string1==string2 如果指定的文字字符串匹配,指定条件为 true。 
EXIST filename    如果指定的文件名存在,指定条件为 true。
command   如果符合条件,指定要执行的命令。

 
@echo offset dir=%~dp0rem 测试字符串是否相等set s="string"if "string"=%s% (echo 字符串相等) else echo 字符串不相等rem 测试字符串是否存在if exist test.bat echo test.bat is existedif not exist test.bat (echo test.bat is no existed) else echo test.bat is existedrem 测试errorlevel (errorlevel 是否大于或等于number,是则执行command)                       xcopy test.bat copy(copy是一个目录)echo %errorlevel%if errorlevel 1 echo 文件拷贝失败if errorlevel 0 echo 文件拷贝成功if errorlevel 5 echo 写盘错误以下就是几个常用命令的返回值及其代表的意义:backup0 备份成功1 未找到备份文件2 文件共享冲突阻止备份完成3 用户用ctrl-c中止备份4 由于致命的错误使备份操作中止diskcomp0 盘比较相同1 盘比较不同2 用户通过ctrl-c中止比较操作3 由于致命的错误使比较操作中止4 预置错误中止比较diskcopy0 盘拷贝操作成功1 非致命盘读/写错2 用户通过ctrl-c结束拷贝操作3 因致命的处理错误使盘拷贝中止4 预置错误阻止拷贝操作format0 格式化成功                     3 用户通过ctrl-c中止格式化处理4 因致命的处理错误使格式化中止5 在提示“proceed with format(y/n)?”下用户键入n结束xcopy0 成功拷贝文件1 未找到拷贝文件2 用户通过ctrl-c中止拷贝操作4 预置错误阻止文件拷贝操作5 拷贝过程中写盘错误   
 
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表