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

但对我来说,这个标签本身就很神秘……他在做什么?dp是指驱动器和路径吗?0是否指包含文件名的批处理文件的路径%0 ?

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

我还想知道它是否是一个文档化的特性,或者是容易被弃用的特性。


当前回答

举个例子就好了,这里有一个简单的例子

for %I in (*.*) do @echo %~xI

它只列出当前文件夹中每个文件的扩展名

对于更多有用的变量组合(也在前面的响应中列出),从CMD提示符执行:HELP for 其中包含以下片段

修饰语可以组合起来得到复合结果:

%~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

其他回答

举个例子就好了,这里有一个简单的例子

for %I in (*.*) do @echo %~xI

它只列出当前文件夹中每个文件的扩展名

对于更多有用的变量组合(也在前面的响应中列出),从CMD提示符执行:HELP for 其中包含以下片段

修饰语可以组合起来得到复合结果:

%~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

调用

for /?

在命令行中提供了关于此语法的帮助(也可以在FOR之外使用,这只是可以找到帮助的地方)。

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 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是批处理文件的完整名称,正如您所假设的那样)。

批处理脚本中的变量%0被设置为正在执行的批处理文件的名称。

%和0之间的~dp特殊语法基本上是说展开变量%0以显示驱动器号和路径,这将为您提供包含批处理文件的当前目录!

Help = Link

(首先,我想推荐这个有用的批量参考网站: 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% will return the entire command line as passed to CMD.EXE %* will return the remainder of the command line starting at the first command line argument (in Windows NT 4, %* also includes all leading spaces) %~dn will return the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC) %~pn will return the directory of %n if %n is a valid path or file name (no UNC) %~nn will return the file name only of %n if %n is a valid file name %~xn will return the file extension only of %n if %n is a valid file name %~fn will return the fully qualified path of %n if %n is a valid file name or directory

加1

刚刚找到了一些关于神秘的~波浪符的很好的参考。

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

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

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

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

例如: (batch.bat)

@echo off
@echo ~dp0= %~dp0
@echo ~dp1= %~dp1
@echo ~dp2= %~dp2
@echo on

1:运行

D:\Workbench>batch arg1 arg2

~dp0= D:\Workbench\
~dp1= D:\Workbench\
~dp2= D:\Workbench\

运行2:

D:\Workbench>batch c:\123\a.exe e:\abc\b.exe

~dp0= D:\Workbench\
~dp1= c:\123\
~dp2= e:\abc\

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

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

C: \ \ test.bat脚本

与内容:

@echo off
echo %~dp0

当你从命令提示符运行它时,你会看到这样的结果:

C: \ \的脚本