DOS批处理简介(上)

文章整理自:<wbr style="line-height:25px"><a target="_blank" rel="nofollow" href="http://hi.baidu.com/yjwkq/blog/item/b5adc801ee94fdd4277fb5b7.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://hi.baidu.com/yjwkq/blog/item/b5adc801ee94fdd4277fb5b7.html</a> <div style="line-height:25px"> <div style="line-height:25px"> <span style="line-height:25px"><span style="font-size:16px; line-height:28px">一.简单内部命令简介</span></span> </div> <div style="line-height:25px"> <span style="line-height:25px">1.Echo 命令</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">打开回显或关闭请求回显功能,或显示消息。如果没有任何参数,echo 命令将显示当前回显</span><span style="color:#003366; line-height:25px">设置。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">语法</span> </div> <div style="line-height:25px"><span style="color:#993300; line-height:25px">echo [{on|off}] [message]</span></div> <div style="line-height:25px"> <span style="line-height:25px">比如</span>:<span style="color:#ff6600; line-height:25px">@echo off / echo hello world</span> </div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">在实际应用中我们会把这条命令和重定向符号(也称为管道符号,一般用&gt; &gt;&gt; ^)结合来实</span><span style="line-height:25px; color:rgb(0,0,128)">现输入一些命令到特定格式的文件中.这将在以后的例子中体现出来。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">2.@ 命令</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">表示不显示@后面的命令</span>。</div> <div style="line-height:25px"> <span style="line-height:25px">比如</span>:<span style="color:#ff6600; line-height:25px">@echo Now initializing the program,please wait a minite...</span> </div> <div style="line-height:25px"> <span style="line-height:25px">3.Goto 命令</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">指定跳转到标签,找到标签后,程序将处理从下一行开始的命令。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">语法</span>:<span style="color:#ff6600; line-height:25px">goto label (label是参数,指定所要转向的批处理程序中的行。)</span> </div> <div style="line-height:25px"> <span style="line-height:25px">比如</span>:</div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">if {%1}=={} goto label0</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">if {%2}=={} goto label0</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">@Rem check parameters if null show usage</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">:label0</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">echo Usage: monitor.bat ServerIP PortNumber</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">goto end</span></div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">标签的名字可以随便起,但是最好是有意义的字母啦,字母前加个:用来表示这个字母是标签,goto命令就是根据这个:来寻找下一步跳到到那里。最好有一些说明这样你别人看起来才会理解你的意图啊。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">注</span>:<span style="color:#000080; line-height:25px">关于标签的更多内容请参照</span>《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011381030871/" style="color:rgb(207,121,28); line-height:25px; text-decoration:underline">函数和标签的使用</a></strong>》</div> <div style="line-height:25px"> <span style="line-height:25px">4.Rem 命令</span> </div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">注释命令,在C语言中相当与/*--------*/,它并不会被执行,只是起一个注释的作用,便于别人阅读和你自己日后修改。</span></div> <div style="line-height:25px"> <span style="line-height:25px">语法</span>:<span style="color:#993300; line-height:25px">Rem Message</span> </div> <div style="line-height:25px"> <span style="line-height:25px">比如</span>:<span style="color:#ff6600; line-height:25px">@Rem Here is the description.</span> </div> <div style="line-height:25px"><span style="line-height:25px">5.Pause 命令</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">运行 Pause 命令时,将显示下面的消息:Press any key to continue . . .</span></div> <div style="line-height:25px"> <span style="line-height:25px">比如</span>:</div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">@echo off</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">:begin</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">copy a:*.* d:\back</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">echo Please put a new disk into driver A</span></div> <div style="line-height:25px"> <span style="color:#ff6600; line-height:25px">pause</span><span style="color:#3366ff; line-height:25px"></span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">goto begin</span></div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">在这个例子中,驱动器 A 中磁盘上的所有文件均复制到d:\back中。显示的注释提示您将另</span><span style="line-height:25px; color:rgb(0,0,128)">一张磁盘放入驱动器 A 时,pause 命令会使程序挂起,以便您更换磁盘,然后按任意键继续</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">处理。</span></div> <div style="line-height:25px"> <span style="line-height:25px">6.Call 命令</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">它用于调用</span><span style="color:#993300; line-height:25px">调用批处理文件</span><span style="line-height:25px; color:rgb(0,51,102)">或</span><span style="color:#993300; line-height:25px">批处理函数</span><span style="color:#003366; line-height:25px">。如果在脚本或批处理文件外使用 Call,它将不会在命令行起作用。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">语法</span> </div> <div style="line-height:25px"><span style="color:#993300; line-height:25px">call [[Drive:][Path] FileName [BatchParameters]] [:label [arguments]]</span></div> <div style="line-height:25px"> <span style="line-height:25px">参数</span> </div> <div style="line-height:25px">[Drive:}[Path] FileName</div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">指定要调用的批处理程序的位置和名称。filename 参数必须具有 .bat 或 .cmd 扩展名。</span> </div> <div style="line-height:25px"> <span style="line-height:22px">注</span>:<span style="color:#000080; line-height:22px">关于批处理函数的更多内容请参照</span>《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011381030871/" style="color:rgb(207,121,28); line-height:22px; text-decoration:underline">函数和标签的使用</a></strong>》</div> <div style="line-height:25px"> <span style="line-height:25px">7.start 命令</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">调用外部程序,所有的DOS命令和命令行程序都可以由start命令来调用。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">常用参数</span>:</div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">MIN</span><span style="color:#000080; line-height:25px">开始时窗口最小化</span> </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">SEPARAT</span><span style="color:#000080; line-height:25px">E 在分开的空间内开始 16 位 Windows 程序</span> </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">HIGH</span><span style="color:#000080; line-height:25px">在 HIGH 优先级类别开始应用程序</span> </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">REALTIME</span><span style="color:#000080; line-height:25px">在 REALTIME 优先级类别开始应用程序</span> </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">WAIT</span><span style="color:#000080; line-height:25px">启动应用程序并等候它结束</span> </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">parameters</span><span style="color:#000080; line-height:25px">这些为传送到命令/程序的参数</span> </div> <div style="line-height:25px"> <span style="line-height:25px">注</span>:<span style="color:#000080; line-height:25px">执行的应用程序是 32-位 GUI 应用程序时,CMD.EXE 不等应用程序终止就返回命令提示。如</span> </div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">果在命令脚本内执行,该新行为则不会发生</span>。</div> <div style="line-height:25px"><span style="line-height:25px">8.choice 命令</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">choice 使用此命令可以让用户输入一个字符,从而运行不同的命令。使用时应该加/c:参数,</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">c:后应写提示可输入的字符,之间无空格。它的返回码为1234……</span></div> <div style="line-height:25px"> <span style="line-height:25px">比如</span>:<span style="color:#ff6600; line-height:25px">choice /c:dme defrag,mem,end</span>将显示</div> <div style="line-height:25px">defrag,mem,end[D,M,E]?</div> <div style="line-height:25px"> <span style="line-height:25px">示例1</span>:</div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">@echo off</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">choice /c:dme defrag,mem,end</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">if errorlevel 3 goto defrag (应先判断数值最高的错误码)</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">if errorlevel 2 goto mem</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">if errotlevel 1 goto end</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">:defrag</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">c:\dos\defrag</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">goto end</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">:mem</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">mem</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">goto end</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">:end</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">echo good bye</span></div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">此文件运行后,将显示 defrag,mem,end[D,M,E]? 用户可选择d m e ,然后if语句将作出</span><span style="line-height:25px; color:rgb(0,0,128)">判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号</span><span style="line-height:25px; color:rgb(0,0,128)">为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">bye,文件结束。</span></div> <div style="line-height:25px"> <span style="line-height:25px">9.If 命令</span> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">if 表示将判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式:</span> </div> <div style="line-height:25px"> <span style="line-height:25px">1、if "参数" == "字符串" 待执行的命</span>令</div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">参数如果等于指定的字符串,则条件成立,运行命令,否则运行下一句。(注意是两个等号)</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">如if "%1"=="a" format a:</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">if {%1}=={} goto noparms</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">if {%2}=={} goto noparms</span></div> <div style="line-height:25px"><span style="line-height:25px">2、if exist 文件名 待执行的命令</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">如果有指定的文件,则条件成立,运行命令,否则运行下一句。</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">如if exist config.sys edit config.sys</span></div> <div style="line-height:25px"><span style="line-height:25px">3、if errorlevel / if not errorlevel 数字 待执行的命令</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。</span></div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">如if errorlevel 2 goto x2</span></div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">DOS程序运行时都会返回一个数字给DOS,称为错误码errorlevel或称返回码,常见的返回</span><span style="color:#003366; line-height:25px">码为0、1。</span> </div> <div style="line-height:25px"> <span style="line-height:25px">注意</span>:关于if命令的更多内容请参照《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011316111534481/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">if命令</a></strong>》和《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201139105426192/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">关于if errorlevel 命令</a></strong>》以及《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201139112057294/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">errorlevel与%errorlevel%的区别</a></strong>》</div> <div style="line-height:25px"> <span style="line-height:25px">10.for 命令</span> </div> </div> <div style="line-height:25px">关于for命令请参照《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920113128413310/" style="color:rgb(207,121,28); line-height:25px; text-decoration:underline">for命令详解(上)</a></strong>》和《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920113129356736/" style="color:rgb(207,121,28); line-height:25px; text-decoration:underline">for命令详解(下)</a></strong>》</div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值