我发现%~dp0非常有用,我经常使用它使我的批处理文件更具可移植性。
但对我来说,这个标签本身就很神秘……他在做什么?dp是指驱动器和路径吗?0是否指包含文件名的批处理文件的路径%0 ?
或者这只是一个奇怪的标签?
我还想知道它是否是一个文档化的特性,或者是容易被弃用的特性。
我发现%~dp0非常有用,我经常使用它使我的批处理文件更具可移植性。
但对我来说,这个标签本身就很神秘……他在做什么?dp是指驱动器和路径吗?0是否指包含文件名的批处理文件的路径%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% 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\
其他回答
来自Strawberry Perl的便携式shell启动器的一个很好的例子:
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在这里做什么,但它很有效!
另一个有很大帮助的技巧是,要将当前目录设置到不同的驱动器,必须先使用%~d0,然后使用cd %~dp0。这将把目录更改为批处理文件的驱动器,然后更改为其文件夹。
或者,对于#oneLinerLovers,正如@Omni在评论中指出的那样,cd /d %~dp0将同时改变驱动器和目录:)
希望这能帮助到一些人。
(首先,我想推荐这个有用的批量参考网站: 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\
批处理脚本中的变量%0被设置为正在执行的批处理文件的名称。
%和0之间的~dp特殊语法基本上是说展开变量%0以显示驱动器号和路径,这将为您提供包含批处理文件的当前目录!
Help = Link
举个例子就好了,这里有一个简单的例子
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