批处理的高级运用技巧

2019-09-19 07:07:23于海丽

defrag,mem,end[D,M,E]? 
Sample: 
Sample.bat的内容如下: 
@echo off 
choice /cme defrag,mem,end 
if errorlevel 3 goto defrag (应先判断数值最高的错误码) 
if errorlevel 2 goto mem 
if errotlevel 1 goto end 
efrag 
c:dosdefrag 
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,文件结束。


批处理的高级运用2
三.如何使用组合命令(Compound Command) 
1.& 
Usage:第一条命令 & 第二条命令 [& 第三条命令...] 
用这种方法可以同时执行多条命令,而不管命令是否执行成功 
Sample: 
C:>dir z: & dir c:Ex4rch 
The system cannot find the path specified. 
Volume in drive C has no label. 
Volume Serial Number is 0078-59FB 
Directory of c:Ex4rch 
2002-05-14 23:51 <DIR> . 
2002-05-14 23:51 <DIR> .. 
2002-05-14 23:51 14 sometips.gif 
2.&& 
Usage:第一条命令 && 第二条命令 [&& 第三条命令...] 
用这种方法可以同时执行多条命令,当碰到执行出错的命令后将不执行后面的命令,如果一直没有出错则一直执行完所有命令; 
Sample: 
C:>dir z: && dir c:Ex4rch 
The system cannot find the path specified. 
C:>dir c:Ex4rch && dir z: 
Volume in drive C has no label. 
Volume Serial Number is 0078-59FB 
Directory of c:Ex4rch 
2002-05-14 23:55 <DIR> . 
2002-05-14 23:55 <DIR> .. 
2002-05-14 23:55 14 sometips.gif 
1 File(s) 14 bytes 
2 Dir(s) 768,671,744 bytes free 
The system cannot find the path specified. 
在做备份的时候可能会用到这种命令会比较简单,如: 
dir file://192.168.0.1/database/backup.mdb && copy file://192.168.0.1/database/backup.mdb E:backup 
如果远程服务器上存在backup.mdb文件,就执行copy命令,若不存在该文件则不执行copy命令。这种用法可以替换IF exist了 :)  3.|| 
相关文章 大家在看