﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-空谷幽人-随笔分类-DOS Batch</title><link>http://www.blogjava.net/wingwing/category/45402.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 22 Jun 2010 03:29:28 GMT</lastBuildDate><pubDate>Tue, 22 Jun 2010 03:29:28 GMT</pubDate><ttl>60</ttl><item><title>（转帖）DOS Batch 命令</title><link>http://www.blogjava.net/wingwing/archive/2010/06/22/324135.html</link><dc:creator>Li Ya Qiang</dc:creator><author>Li Ya Qiang</author><pubDate>Tue, 22 Jun 2010 03:20:00 GMT</pubDate><guid>http://www.blogjava.net/wingwing/archive/2010/06/22/324135.html</guid><wfw:comment>http://www.blogjava.net/wingwing/comments/324135.html</wfw:comment><comments>http://www.blogjava.net/wingwing/archive/2010/06/22/324135.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wingwing/comments/commentRss/324135.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wingwing/services/trackbacks/324135.html</trackback:ping><description><![CDATA[<span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><strong><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">1</span>
echo 和 @</span></strong></span><br />
</span><strong><span style="color: rgb(139, 0, 0);">回显命令<br />
</span></strong><span style="color: rgb(0, 0, 255);">@&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#关闭单行回显<br />
echo off&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;#从下一行开始关闭回显<br />
@echo off&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #从本行开始关闭回显。一般批处理第一行都是这个<br />
echo
on&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#从下一行开始打开回显<br />
echo&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示当前是 echo off
状态还是 echo on 状态<br />
echo.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#输出一个&#8221;回车换行&#8221;，空白行<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #(同echo, echo; echo+ echo[ echo] echo/ echo)</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">2</span> errorlevel</span></span></strong><br />
<span style="color: rgb(0, 0, 255);">echo %errorlevel%<br />
每个命令运行结束，可以用这个命令行格式
查看返回码<br />
默认值为0，一般命令执行出错会设 errorlevel 为1</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">3</span> dir</span></span></strong><br />
<strong><span style="color: rgb(255, 0, 0);"><span style="color: rgb(139, 0, 0);">显示文
件夹内容</span><br />
</span></strong><span style="color: rgb(0, 0, 255);">dir&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示当前目录中的文件和子目录<br />
dir /a&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;#显示当前目录中的文件和子目录，包括隐藏文件和系统文件<br />
dir c: /a:d&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #显示 C 盘当前目录中的目录<br />
dir
c: /a:-d&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示 C 盘根目录中的文件<br />
dir c: /b/p&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#/b只显示文件名，/p分页显示<br />
dir
*.exe /s&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示当前目录和子目录里所有的.exe文件</span><br />
<br />
<span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><strong><span style="color: rgb(0, 0, 0);">4</span> cd</strong></span></span><br />
<span style="color: rgb(139, 0, 0);"><strong>切换目录</strong><br />
</span><span style="color: rgb(0, 0, 255);">cd&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#进入根目录<br />
cd&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #显示当前目录<br />
cd /d d:sdk&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#可以同时更改盘符和目录</span><br />
<br />
<span style="color: rgb(0, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">5</span>
md</span></span></strong></span><br />
<span style="color: rgb(139, 0, 0);"><strong>创建目录</strong><br />
</span><span style="color: rgb(0, 0, 255);">md d:abc&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #如果 d:a 不存在，将会自动创建中级目录<br />
#如果命令扩展名被停用，则需要键入
mkdir abc。<br />
</span><br />
<span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><strong><span style="color: rgb(0, 0, 0);">6</span> rd</strong></span></span><br />
<span style="color: rgb(139, 0, 0);"><strong>删除目录</strong><br />
</span><span style="color: rgb(0, 0, 255);">rd abc&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#删除当前目录里的 abc 子目录，要求为空目录<br />
rd /s/q d:temp&nbsp;
&nbsp;&nbsp; &nbsp;#删除 d:temp 文件夹及其子文件夹和文件，/q安静模式</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">7</span> del</span></span></strong><br />
<strong><span style="color: rgb(255, 0, 0);"><span style="color: rgb(139, 0, 0);">删除文
件</span><br />
</span></strong><span style="color: rgb(0, 0, 255);">del
d:test.txt&nbsp; &nbsp;&nbsp; &nbsp;#删除指定文件，不能是隐藏、系统、只读文件<br />
del /q/a/f d:temp*.*<br />
删除
d:temp 文件夹里面的所有文件，包括隐藏、只读、系统文件，不包括子目录<br />
del /q/a/f/s d:temp*.*<br />
删除
d:temp 及子文件夹里面的所有文件，包括隐藏、只读、系统文件，不包括子目录</span><br />
<br />
<span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><strong><span style="color: rgb(0, 0, 0);">8</span> ren</strong></span><br />
</span><span style="color: rgb(255, 0, 0);"><strong><span style="color: rgb(139, 0, 0);">重命名命令</span></strong></span><br />
<span style="color: rgb(0, 0, 255);">ren d:temp tmp&nbsp; &nbsp;&nbsp; &nbsp;#支持对文件夹的重命名<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">9</span> cls</span><br />
</span></strong><span style="color: rgb(139, 0, 0);"><strong>清屏<br />
</strong></span><br />
<span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><strong><span style="color: rgb(0, 0, 0);">10</span> type<br />
</strong></span></span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>显
示文件内容<br />
</strong></span>type c:boot.ini&nbsp; &nbsp;&nbsp;&nbsp;#显示指定文件的内容，程序文件一般会显示乱码<br />
type
*.txt&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#显示当前目录里所有.txt文件的内容</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">11</span> copy</span><br />
</span></strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>拷
贝文件<br />
</strong></span>copy c:test.txt d:test.bak<br />
复制 c:test.txt 文件到
d: ，并重命名为 test.bak<br />
copy con test.txt<br />
从屏幕上等待输入，按 Ctrl+Z
结束输入，输入内容存为test.txt文件<br />
con代表屏幕，prn代表打印机，nul代表空设备<br />
copy 1.txt + 2.txt
3.txt<br />
合并 1.txt 和 2.txt 的内容，保存为 3.txt 文件<br />
如果不指定 3.txt ，则保存到 1.txt<br />
copy
test.txt +<br />
复制文件到自己，实际上是修改了文件日期</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">12</span> title</span><br />
</span></strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>设
置cmd窗口的标题</strong></span><br />
title 新标题&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#可以看到cmd窗口的标题栏变了</span><br />
<br />
<span style="color: rgb(255, 0, 0);"><span style="font-size: medium;"><strong><span style="color: rgb(0, 0, 0);">13</span> ver</strong><br />
</span></span><span style="color: rgb(139, 0, 0);"><strong>显示系统版本<br />
</strong></span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">14</span> label 和 vol</span><br />
</span></strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>设
置卷标<br />
</strong></span>vol&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示卷标<br />
label&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#显示卷标，同时提示输入新卷标<br />
label c:system&nbsp; &nbsp;&nbsp; &nbsp; #设置C盘的卷标为 system<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">15</span> pause</span></span><br />
</strong><span style="color: rgb(139, 0, 0);"><strong>暂停命令<br />
</strong></span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">16</span> rem 和 ::</span></span><br />
</strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>注
释命令</strong></span><br />
注释行不执行操作<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">17</span> date 和 time</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>日
期和时间</strong></span><br />
date&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#显示当前日期，并提示输入新日期，按"回车"略过输入<br />
date/t&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#只显示当前日期，不提示输入新日期<br />
time&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#显示当前时间，并提示输入新时间，按"回车"略过输入<br />
time/t&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#只显示当前时间，不提示输入新时间</span><br />
<br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">18</span> goto 和 :</span><br />
</strong></span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>跳
转命令<br />
</strong></span>:label&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#行首为:表示该行是标签行，标签行不执行操作<br />
goto
label&nbsp; &nbsp;&nbsp;&nbsp;#跳转到指定的标签那一行</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">19</span> find (外部命令)</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>查
找命令<br />
</strong></span>find "abc" c:test.txt<br />
在 c:test.txt 文件里查找含 abc
字符串的行<br />
如果找不到，将设 errorlevel 返回码为1<br />
find /i &#8220;abc&#8221; c:test.txt<br />
查找含
abc 的行，忽略大小写<br />
find /c "abc" c:test.txt<br />
显示含 abc 的行的行数</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">20</span> more (外部命令)</span></span><br />
</strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>逐
屏显示<br />
</strong></span>more c:test.txt&nbsp; &nbsp;&nbsp;&nbsp;#逐屏显示 c:test.txt 的文件内容</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">21</span> tree</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>显
示目录结构<br />
</strong></span>tree d:&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #显示D盘的文件目录结构</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">22</span> &amp;</span></span></strong><br />
<span style="color: rgb(139, 0, 0);"><strong>顺序执行多条命令，而不管命令是否执行成功<br />
</strong></span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">23</span> &amp;&amp;</span><br />
</span></strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>顺
序执行多条命令，当碰到执行出错的命令后将不执行后面的命令<br />
</strong></span>find "ok" c:test.txt
&amp;&amp; echo 成功<br />
如果找到了"ok"字样，就显示"成功"，找不到就不显示</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">24</span> ||</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>顺
序执行多条命令，当碰到执行正确的命令后将不执行后面的命令</strong></span><br />
find "ok" c:test.txt ||
echo 不成功<br />
如果找不到"ok"字样，就显示"不成功"，找到了就不显示<br />
</span><br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">25</span> |</span></strong><br />
</span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>管
道命令</strong></span><br />
dir *.* /s/a | find /c ".exe"<br />
管道命令表示先执行 dir
命令，对其输出的结果执行后面的 find 命令<br />
该命令行结果：输出当前文件夹及所有子文件夹里的.exe文件的个数<br />
type
c:test.txt|more<br />
这个和 more c:test.txt 的效果是一样的</span><br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">26</span> &gt; 和 &gt;&gt;</span></span><br />
</strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>输
出重定向命令<br />
</strong></span>&gt; 清除文件中原有的内容后再写入<br />
&gt;&gt;
追加内容到文件末尾，而不会清除原有的内容<br />
主要将本来显示在屏幕上的内容输出到指定文件中<br />
指定文件如果不存在，则自动生成该文件<br />
type
c:test.txt &gt;prn<br />
屏幕上不显示文件内容，转向输出到打印机<br />
echo hello world&gt;con<br />
在
屏幕上显示hello world，实际上所有输出都是默认 &gt;con 的<br />
copy c:test.txt f: &gt;nul<br />
拷
贝文件，并且不显示"文件复制成功"的提示信息，但如果f盘不存在，还是会显示出错信息<br />
copy c:test.txt f: &gt;nul
2&gt;nul<br />
不显示&#8221;文件复制成功&#8221;的提示信息，并且f盘不存在的话，也不显示错误提示信息<br />
echo ^^W ^&gt;
^W&gt;c:test.txt<br />
生成的文件内容为 ^W &gt; W<br />
^ 和 &gt;
是控制命令，要把它们输出到文件，必须在前面加个 ^ 符号<br />
</span><br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">27</span> &lt;</span><br />
</strong></span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>从
文件中获得输入信息，而不是从屏幕上</strong></span><br />
一般用于 date time label 等需要等待输入的命令<br />
@echo
off<br />
echo 2005-05-01&gt;temp.txt<br />
date &lt;temp.txt<br />
del temp.txt<br />
这
样就可以不等待输入直接修改当前日期<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">28</span>
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %*</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>命
令行传递给批处理的参数</strong></span><br />
%0 批处理文件本身<br />
%1 第一个参数<br />
%9 第九个参数<br />
%*
从第一个参数开始的所有参数<br />
<br />
批参数(%n)的替代已被增强。您可以使用以下语法:<br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;%~1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; -
删除引号(")，扩充 %1<br />
&nbsp; &nbsp;&nbsp;&nbsp;%~f1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 将 %1 扩充到一个完全合格的路径名<br />
&nbsp; &nbsp;&nbsp;&nbsp;%~d1&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 仅将 %1 扩充到一个驱动器号<br />
&nbsp; &nbsp;&nbsp;&nbsp;%~p1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 仅将 %1 扩充到一个路径<br />
&nbsp;
&nbsp;&nbsp;&nbsp;%~n1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 仅将 %1 扩充到一个文件名<br />
&nbsp; &nbsp;&nbsp;&nbsp;%~x1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 仅将 %1 扩充到一个文件扩展名<br />
&nbsp;
&nbsp;&nbsp;&nbsp;%~s1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 扩充的路径指含有短名<br />
&nbsp; &nbsp;&nbsp;&nbsp;%~a1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 将 %1 扩充到文件属性<br />
&nbsp;
&nbsp;&nbsp;&nbsp;%~t1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 将 %1 扩充到文件的日期/时间<br />
&nbsp; &nbsp;&nbsp;&nbsp;%~z1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;- 将 %1 扩充到文件的大小<br />
&nbsp;
&nbsp;&nbsp;&nbsp;%~$PATH : 1 - 查找列在 PATH 环境变量的目录，并将 %1<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
扩充到找到的第一个完全合格的名称。如果环境<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 变量名未被定义，或者没有找到文件，此组合键会<br />
&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 扩充到空字符串<br />
<br />
可以组合修定符来取得多重结果:<br />
<br />
&nbsp; &nbsp; %~dp1&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;- 只将 %1 扩展到驱动器号和路径<br />
&nbsp; &nbsp; %~nx1&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;- 只将 %1 扩展到文件名和扩展名<br />
&nbsp; &nbsp;
%~dp$PATH:1 - 在列在 PATH 环境变量中的目录里查找 %1，<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
并扩展到找到的第一个文件的驱动器号和路径。<br />
&nbsp; &nbsp; %~ftza1&nbsp; &nbsp;&nbsp; &nbsp;- 将 %1 扩展到类似 DIR 的输出行。<br />
可以参照
call/? 或 for/? 看出每个参数的含意<br />
echo load "%%1" "%%2"&gt;c:test.txt<br />
生成的文
件内容为 load "%1" "%2"<br />
批处理文件里，用这个格式把命令行参数输出到文件<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">29</span> if</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>判
断命令<br />
</strong></span>if "%1"=="/a" echo 第一个参数是/a<br />
if /i "%1" equ
"/a" echo 第一个参数是/a<br />
/i 表示不区分大小写，equ 和 == 是一样的，其它运算符参见 if/?<br />
if exist
c:test.bat echo 存在c:test.bat文件<br />
if not exist c:windows (<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo
不存在c:windows文件夹<br />
&nbsp; &nbsp;&nbsp;&nbsp;)<br />
if exist c:test.bat (<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo
存在c:test.bat<br />
&nbsp; &nbsp;&nbsp;&nbsp;) else (<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo 不存在c:test.bat<br />
&nbsp; &nbsp;&nbsp;&nbsp;)<br />
<br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">30</span> setlocal 和 endlocal</span></span></strong><br />
<span style="color: rgb(139, 0, 0);"><strong>设置&#8221;命令扩展名&#8221;和&#8221;延缓环境变量扩充&#8221;</strong></span><br />
SETLOCAL
ENABLEEXTENSIONS&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #启用"命令扩展名"<br />
SETLOCAL DISABLEEXTENSIONS&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#停用"命令扩展名"<br />
SETLOCAL ENABLEDELAYEDEXPANSION&nbsp; &nbsp;&nbsp; &nbsp;
#启用"延缓环境变量扩充"<br />
SETLOCAL DISABLEDELAYEDEXPANSION&nbsp; &nbsp;&nbsp; &nbsp;#停用"延缓环境变量扩充"<br />
ENDLOCAL&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#恢复到使用SETLOCAL语句以前的状态<br />
&#8220;命令扩展名&#8221;默认为启用<br />
&#8220;延
缓环境变量扩充&#8221;默认为停用<br />
批处理结束系统会自动恢复默认值<br />
可以修改注册表以禁用"命令扩展名"，详见 cmd /?
。所以用到"命令扩展名"的程<br />
序，建议在开头和结尾加上 SETLOCAL ENABLEEXTENSIONS 和 ENDLOCAL
语句，以确<br />
保程序能在其它系统上正确运行<br />
"延缓环境变量扩充"主要用于 if 和 for 的符合语句，在 set
的说明里有其实用例程<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">31</span>
set</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>设置变量<br />
</strong></span>引用变量可在变量
名前后加 % ，即 %变量名%<br />
set&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示目前所有可用的变量，包括系统变量和自定义的变量<br />
echo
%SystemDrive%&nbsp; &nbsp;&nbsp; &nbsp;#显示系统盘盘符。系统变量可以直接引用<br />
set p&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#显示所有以p开头的变量，要是一个也没有就设errorlevel=1<br />
set p=aa1bb1aa2bb2&nbsp; &nbsp;&nbsp;
&nbsp;#设置变量p，并赋值为 = 后面的字符串，即aa1bb1aa2bb2<br />
echo %p%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#显示变量p代表的字符串，即aa1bb1aa2bb2<br />
echo %p:~6%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#显示变量p中第6个字符以后的所有字符，即aa2bb2<br />
echo %p:~6,3%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;#显示第6个字符以后的3个字符，即aa2<br />
echo %p:~0,3%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#显示前3个字符，即aa1<br />
echo
%p:~-2%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示最后面的2个字符，即b2<br />
echo %p:~0,-2%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#显示除了最后2个字符以外的其它字符，即aa1bb1aa2b<br />
echo %p:aa=c%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;#用c替换变量p中所有的aa，即显示c1bb1c2bb2<br />
echo %p:aa=%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;#将变量p中的所有aa字符串置换为空，即显示1bb12bb2<br />
echo %p:*bb=c%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#第一个bb及其之前的所有字符被替换为c，即显示c1aa2bb2<br />
set p=%p:*bb=c%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#设置变量p，赋值为
%p:*bb=c% ，即c1aa2bb2<br />
set /a p=39&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #设置p为数值型变量，值为39<br />
set
/a p=39/10&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #支持运算符，有小数时用去尾法，39/10=3.9，去尾得3，p=3<br />
set /a p=p/10&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#用 /a 参数时，在 = 后面的变量可以不加%直接引用<br />
set /a p=&#8221;1&amp;0&#8243;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#&#8221;与&#8221;运算，要加引号。其它支持的运算符参见set/?<br />
set p=&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#取消p变量<br />
set /p
p=请输入<br />
屏幕上显示&#8221;请输入&#8221;，并会将输入的字符串赋值给变量p<br />
注意这条可以用来取代 choice 命令<br />
注意变量在 if 和
for 的复合语句里是一次性全部替换的，如<br />
@echo off<br />
set p=aaa<br />
if %p%==aaa (<br />
&nbsp;
&nbsp;&nbsp;&nbsp;echo %p%<br />
&nbsp; &nbsp;&nbsp;&nbsp;set p=bbb<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo %p%<br />
&nbsp; &nbsp;&nbsp;&nbsp;)<br />
结果将显示<br />
aaa<br />
aaa<br />
因
为在读取 if 语句时已经将所有 %p% 替换为aaa<br />
这里的"替换"，在 /? 帮助里就是指"扩充"、"环境变量扩充"<br />
可以启用&#8221;
延缓环境变量扩充&#8221;，用 ! 来引用变量，即 !变量名!<br />
@echo off<br />
SETLOCAL
ENABLEDELAYEDEXPANSION<br />
set p=aaa<br />
if %p%==aaa (<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo %p%<br />
&nbsp;
&nbsp;&nbsp;&nbsp;set p=bbb<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo !p!<br />
&nbsp; &nbsp;&nbsp;&nbsp;)<br />
ENDLOCAL<br />
结果将显示<br />
aaa<br />
bbb<br />
还
有几个动态变量，运行 set 看不到<br />
%CD%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #代表当前目录的字符串<br />
%DATE%&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#当前日期<br />
%TIME%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#当前时间<br />
%RANDOM%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;#随机整数，介于0~32767<br />
%ERRORLEVEL%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#当前 ERRORLEVEL 值<br />
%CMDEXTVERSION%&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#当前命令处理器扩展名版本号<br />
%CMDCMDLINE%&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#调用命令处理器的原始命令行<br />
可以用
echo命令查看每个变量值，如 echo %time%<br />
注意 %time% 精确到毫秒，在批处理需要延时处理时可以用到<br />
</span><br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">32</span> start</span></strong><br />
</span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>批
处理中调用外部程序的命令，否则等外部程序完成后才继续执行剩下的指令</strong></span><br />
</span><br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">33 </span>call</span></strong><br />
</span><span style="color: rgb(139, 0, 0);"><strong>批处理中调用另外一个批处理的命令，否则剩下的批处理指令将不会被执
行<br />
有时有的应用程序用start调用出错的，也可以call调用<br />
</strong></span><br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">34</span> choice (外部命令)</span></strong><br />
</span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>选
择命令<br />
</strong></span>让用户输入一个字符，从而选择运行不同的命令，返回码errorlevel为1234&#8230;&#8230;<br />
win98
里是choice.com<br />
win2000pro里没有，可以从win98里拷过来<br />
win2003里是choice.exe<br />
choice
/N /C y /T 5 /D y&gt;nul<br />
延时5秒</span><br />
<br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">35</span> assoc 和 ftype</span></strong><br />
</span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);">文件关
联<br />
</span>assoc 设置'文件扩展名'关联，关联到'文件类型'<br />
ftype 设置'文件类型'关联，关联到'执行程序和参数'<br />
当
你双击一个.txt文件时，windows并不是根据.txt直接判断用 notepad.exe 打开<br />
而是先判断.txt属于 txtfile
'文件类型'<br />
再调用 txtfile 关联的命令行 txtfile=%SystemRoot%system32NOTEPAD.EXE %1<br />
可
以在"文件夹选项"&#8594;"文件类型"里修改这2种关联<br />
assoc&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示所有'文件扩展名'关联<br />
assoc
.txt&nbsp; &nbsp;&nbsp; &nbsp; #显示.txt代表的'文件类型'，结果显示 .txt=txtfile<br />
assoc .doc&nbsp; &nbsp;&nbsp; &nbsp;
#显示.doc代表的'文件类型'，结果显示 .doc=Word.Document.8<br />
assoc .exe&nbsp; &nbsp;&nbsp; &nbsp;
#显示.exe代表的'文件类型'，结果显示 .exe=exefile<br />
ftype&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#显示所有'文件类型'关联<br />
ftype
exefile&nbsp; &nbsp; #显示exefile类型关联的命令行，结果显示 exefile="%1" %* <br />
assoc
.txt=Word.Document.8<br />
设置.txt为word类型的文档，可以看到.txt文件的图标都变了<br />
assoc
.txt=txtfile<br />
恢复.txt的正确关联<br />
ftype exefile="%1" %*<br />
恢复 exefile 的正确关联<br />
如
果该关联已经被破坏，可以运行 command.com ，再输入这条命令<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">36</span> pushd 和 popd</span></span></strong><br />
<span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>切
换当前目录<br />
</strong></span>@echo off<br />
c: &amp; cd &amp; md mp3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#在
C: 建立 mp3 文件夹<br />
md d:mp4&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #在 D: 建立 mp4 文件夹<br />
cd /d
d:mp4&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #更改当前目录为 d:mp4<br />
pushd c:mp3&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
#保存当前目录，并切换当前目录为 c:mp3<br />
popd&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;#恢复当前目录为刚才保存的 d:mp4<br />
</span><br />
<span style="color: rgb(255, 0, 0);"><strong><span style="font-size: medium;"><span style="color: rgb(0, 0, 0);">37</span> for</span></strong><br />
</span><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>循
环命令<br />
</strong></span>这个比较复杂，请对照 for/? 来看<br />
for %%i in (c: d: e: f:)
do echo %%i<br />
依次调用小括号里的每个字符串，执行 do 后面的命令<br />
注意%%i，在批处理中 for 语句调用参数用2个%<br />
默
认的字符串分隔符是"空格键"，"Tab键"，"回车键"<br />
for %%i in (*.txt) do find "abc" %%i<br />
对
当前目录里所有的txt文件执行 find 命令<br />
for /r . %%i in (*.txt) do find "abc" %%i<br />
在
当前目录和子目录里所有的.txt文件中搜索包含 abc 字符串的行<br />
for /r . %%i in (.) do echo %%~pni<br />
显
示当前目录名和所有子目录名，包括路径，不包括盘符<br />
for /r d:mp3 %%i in (*.mp3) do echo
%%i&gt;&gt;d:mp3.txt<br />
把 d:mp3 及其子目录里的mp3文件的文件名都存到 d:mp3.txt 里去<br />
for
/l %%i in (2,1,8) do echo %%i<br />
生成2345678的一串数字，2是数字序列的开头，8是结尾，1表示每次加1<br />
for
/f %%i in ('set') do echo %%i<br />
对 set 命令的输出结果循环调用，每行一个<br />
for /f
"eol=P" %%i in ('set') do echo %%i<br />
取 set 命令的输出结果，忽略以 P 开头的那几行<br />
for
/f %%i in (d:mp3.txt) do echo %%i<br />
显示 d:mp3.txt 里的每个文件名，每行一个，不支持带空格的名称<br />
for
/f "delims=" %%i in (d:mp3.txt) do echo %%i<br />
显示 d:mp3.txt
里的每个文件名，每行一个，支持带空格的名称<br />
for /f "skip=5 tokens=4" %%a in ('dir') do echo
%%a<br />
对 dir 命令的结果，跳过前面5行，余下的每行取第4列<br />
每列之间的分隔符为默认的"空格"<br />
可以注意到 dir
命令输出的前5行是没有文件名的<br />
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do
(<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo %%a<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo %%b<br />
&nbsp; &nbsp;&nbsp;&nbsp;echo %%c<br />
&nbsp; &nbsp;&nbsp;&nbsp;)<br />
对
date /t 的输出结果，每行取1、2、3列<br />
第一列对应指定的 %%a ，后面的 %%b 和 %%c 是派生出来的，对应其它列<br />
分
隔符指定为 - 和"空格"，注意 delims=- 后面有个"空格"<br />
其中 tokens=1,2,3 若用 tokens=1-3
替换，效果是一样的<br />
for /f "tokens=2* delims=- " %%a in ('date /t') do echo %%b<br />
取
第2列给 %%a ，其后的列都给 %%b</span><br />
<span style="color: rgb(0, 0, 255);"><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">38</span> subst (外部命令)</span></span></strong><br />
<strong><span style="color: rgb(139, 0, 0);">映射磁盘。</span></strong><br />
subst z:
serverd&nbsp; &nbsp;&nbsp; &nbsp;#这样输入z:就可以访问serverd了<br />
subst z: /d&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#取消该映射<br />
subst&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#显示目前所有的映时<br />
</span><br />
<strong><span style="font-size: medium;"><span style="color: rgb(255, 0, 0);"><span style="color: rgb(0, 0, 0);">39</span>&nbsp; &nbsp;xcopy (外部命令)</span></span><br />
</strong><span style="color: rgb(0, 0, 255);"><span style="color: rgb(139, 0, 0);"><strong>文
件拷贝</strong><br />
</span>xcopy d:mp3 e:mp3 /s/e/i/y<br />
复制 d:mp3
文件夹、所有子文件夹和文件到 e: ，覆盖已有文件<br />
加 /i 表示如果 e: 没有 mp3 文件夹就自动新建一个，否则会有询问</span>
<img src ="http://www.blogjava.net/wingwing/aggbug/324135.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wingwing/" target="_blank">Li Ya Qiang</a> 2010-06-22 11:20 <a href="http://www.blogjava.net/wingwing/archive/2010/06/22/324135.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>