What does %~dp0 mean, and how does it work?
我发现
但标签本身对我来说似乎很神秘......
或者它只是一个奇怪的标签?
我还想知道它是否是一个记录的功能,或者是一些容易被弃用的东西。
调用
1 | for /? |
在命令行中提供有关此语法的帮助(也可以在FOR之外使用,这只是可以找到帮助的地方)。
In addition, substitution of FOR
variable references has been enhanced.
You can now use the following optional
syntax:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 %~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 stringThe modifiers can be combined to get
compound results:
1
2
3
4
5
6
7 %~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 lineIn the above examples %I and PATH can
be replaced by other valid values.
The %~ syntax is terminated by a valid
FOR variable name. Picking upper case
variable names like %I makes it more
readable and avoids confusion with the
modifiers, which are not case
sensitive.
可以使用不同的字母,如
(首先,我想为批次推荐这个有用的参考站点:
http://ss64.com/nt/)
然后只是另一个有用的解释:http://htipe.wordpress.com/2008/10/09/the-dp0-variable/
The %~dp0 Variable
The %~dp0 (that’s a zero) variable when referenced within a Windows
batch file will expand to the drive letter and path of that batch
file.The variables %0-%9 refer to the command line parameters of the batch
file. %1-%9 refer to command line arguments after the batch file name.
%0 refers to the batch file itself.If you follow the percent character (%) with a tilde character (~),
you can insert a modifier(s) before the parameter number to alter the
way the variable is expanded. The d modifier expands to the drive
letter and the p modifier expands to the path of the parameter.Example: Let’s say you have a directory on C: called bat_files, and
in that directory is a file called example.bat. In this case, %~dp0
(combining the d and p modifiers) will expand to C:\bat_files.Check out this Microsoft article for a full explanation.
Also, check out this forum thread.
这里有一个更明确的参考:
-
%CmdCmdLine% 将返回传递给CMD.EXE的整个命令行 -
%* 将从第一个命令行参数开始返回命令行的其余部分(在Windows NT 4中,%*还包括所有前导空格) -
如果%n是有效路径或文件名(无UNC),则
%~dn 将返回%n的驱动器号(n的范围为0到9) -
如果%n是有效路径或文件名(无UNC),
%~pn 将返回%n目录 -
如果%n是有效文件名,则
%~nn 将仅返回%n的文件名 -
如果%n是有效文件名,
%~xn 将仅返回%n的文件扩展名 -
如果%n是有效的文件名或目录,则
%~fn 将返回%n的完全限定路径
添加1
刚刚为神秘的
添加2 - 下午1:12 PM 7/6/2018
例如:
(batch.bat)
1 2 3 4 5 | @echo off @echo ~dp0= %~dp0 @echo ~dp1= %~dp1 @echo ~dp2= %~dp2 @echo on |
运行1:
1 2 3 4 5 | D:\Workbench>batch arg1 arg2 ~dp0= D:\Workbench\ ~dp1= D:\Workbench\ ~dp2= D:\Workbench\ |
运行2:
1 2 3 4 5 | D:\Workbench>batch c:\123\a.exe e:\abc\b.exe ~dp0= D:\Workbench\ ~dp1= c:\123\ ~dp2= e:\abc\ |
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
批处理脚本中的变量
%~dp0扩展到正在运行的批处理文件的当前目录路径。
为了清楚地理解,让我们在目录中创建一个批处理文件。
C:\script\test.bat
内容:
1 2 | @echo off echo %~dp0 |
从命令提示符运行它时,您将看到以下结果:
C:\script\
另一个有用的提示是,要将当前目录设置为不同的驱动器,必须先使用
或者,对于#oneLinerLovers,正如@Omni在注释中指出的
希望这有助于某人。
Strawberry Perl的便携式外壳发射器就是一个很好的例子:
1 2 3 4 5 | set drive=%~dp0 set drivep=%drive% if #%drive:~-1%# == #\# set drivep=%drive:~0,-1% set PATH=%drivep%\perl\site\bin;%drivep%\perl\bin;%drivep%\c\bin;%PATH% |
不知道负面的1在那里做什么,但它有效!
一个例子很好 - 这是一个微不足道的
1 | for %I in (*.*) do @echo %~xI |
它仅列出当前文件夹中每个文件的EXTENSIONS
对于CMD提示中更有用的变量组合(也在前面的响应中列出),执行:
其中包含此代码段
可以组合修饰符以获得复合结果:
1 2 3 4 5 6 7 | %~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 |