关于批处理文件:%~dp0是什么意思,它是如何工作的?

What does %~dp0 mean, and how does it work?

我发现%~dp0非常有用,我使用它来使我的批处理文件更具可移植性。

但标签本身对我来说似乎很神秘...... ~在做什么? dp是否意味着驱动器和路径? 0是否指向包含文件名的批处理文件的路径%0

或者它只是一个奇怪的标签?

我还想知道它是否是一个记录的功能,或者是一些容易被弃用的东西。


调用

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 string

The 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 line

In 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.

可以使用不同的字母,如f表示"完整路径名",d表示驱动器号,p表示路径,可以组合使用。 %~是每个序列的开头,数字I表示它适用于参数%I(其中%0是批处理文件的完整名称,就像您假设的那样)。


(首先,我想为批次推荐这个有用的参考站点:
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

刚刚为神秘的~代字号运算符找到了一些很好的参考。

%~字符串称为百分比波浪号运算符。您可以在以下情况下找到它:%~0

:~字符串称为冒号波浪号运算符。你可以找到它像%SOME_VAR:~0,-1%

添加2 - 下午1:12 PM 7/6/2018

%1-%9请参阅命令行参数。如果它们不是有效的路径值,%~dp1 - %~dp9将全部扩展为与%~dp0相同的值。但如果它们是有效的路径值,它们将扩展为自己的驱动程序/路径值。

例如:
(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

批处理脚本中的变量%0设置为正在执行的批处理文件的名称。 %0之间的~dp特殊语法基本上表示扩展变量%0以显示驱动器号和路径,这将为您提供包含批处理文件的当前目录!


%~dp0扩展到正在运行的批处理文件的当前目录路径。

为了清楚地理解,让我们在目录中创建一个批处理文件。

C:\script\test.bat

内容:

1
2
@echo off
echo %~dp0

从命令提示符运行它时,您将看到以下结果:

C:\script\


另一个有用的提示是,要将当前目录设置为不同的驱动器,必须先使用%~d0,然后再使用cd %~dp0。这会将目录更改为批处理文件的驱动器,然后更改为其文件夹。

或者,对于#oneLinerLovers,正如@Omni在注释中指出的cd /d %~dp0将更改驱动器和目录:)

希望这有助于某人。


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提示中更有用的变量组合(也在前面的响应中列出),执行:HELP FOR
其中包含此代码段

可以组合修饰符以获得复合结果:

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