Posted on 2011-06-10 13:37
魔のkyo 阅读(2027)
评论(0) 编辑 收藏 引用
假设有这样一个情况,我们有一个命令行程序(prog.exe)可以处理文件,并将处理结果输出成另一个文件。
在程序的运行参数中指定输入输出的文件,使用方式如下:
prog.exe sample.in sample.out
现在需要写一个批处理利用这个程序处理一批的文件,假设这批文件的扩展名都是.in,而我希望输出的文件就在相同路径下,相同文件名,扩展名改成.out,
批处理可以轻松搞定这点,
在控制台FOR的语法帮助中提及了下面一段话,是对FOR的变量引用的增强方式,事实上这些增强方式对于批处理参数也是支持的,即可以作用在%0,%1,...上
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
将下述代码存成.bat放在任意路径下运行有助于理解d、p、n、x的含义
@echo off
echo (d)rive: %~d0
echo (p)ath: %~p0
echo (n)ame: %~n0
echo e(x)t: %~x0
echo %~dpnx0
pause