PC6
PC6
PC6
PC6 软件站整理 www.pc6.com
www.pc6.com
www.pc6.com
www.pc6.com
Sample :
Sample.bat 的内容如下 :
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag (应先判断数值最高的错误码)
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
此文件运行后,将显示 defrag,mem,end[D,M,E]? 用户可选择 d m e ,然后 if 语句将
作出判断 , d 表示执行标号为 defrag 的程序段 , m 表示执行标号为 mem 的程序段 , e 表
示执行标号为 end 的程序段,每个程序段最后都以 goto end 将程序跳到 end 标号处 , 然
后程序将显示 good bye ,文件结束。
9.If
9.If
9.If
9.If 命令
if 表示将判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式 :
1 、 if " 参数 " == " 字符串 " 待执行的命令
参数如果等于指定的字符串 , 则条件成立 , 运行命令 , 否则运行下一句 。 ( 注意是两个等号
)
如 if "%1"=="a" format a:
if {%1}=={} goto noparms
if {%2}=={} goto noparms
2 、 if exist 文件名 待执行的命令
如果有指定的文件,则条件成立,运行命令,否则运行下一句。
如 if exist config.sys edit config.sys
3 、 if errorlevel / if not errorlevel 数字 待执行的命令
如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。
如 if errorlevel 2 goto x2
DOS 程序运行时都会返回一个数字给 DOS ,称为错误码 errorlevel 或称返回码,常见的
返回码为 0 、 1 。
10.for
10.for
10.for
10.for 命令