Batch file %dp0是什么意思?它是如何工作的?

Batch file %dp0是什么意思?它是如何工作的?,batch-file,Batch File,我发现%~dp0非常有用,我经常使用它来提高批处理文件的可移植性 但标签本身对我来说似乎很神秘。。。~在做什么?dp是否表示驱动器和路径?0是否指包含文件名的批处理文件路径%0 或者只是一个奇怪的标签 我还想知道它是一个文档化的功能,还是一些容易被弃用的功能。 for /? 在命令行中提供了有关此语法的帮助(也可以在外部使用,这正是可以找到帮助的地方) 此外,替代 变量引用已得到增强。 您现在可以使用以下可选选项 语法: %~I - expands %I removing an

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

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

或者只是一个奇怪的标签

我还想知道它是一个文档化的功能,还是一些容易被弃用的功能。

for /?
在命令行中提供了有关此语法的帮助(也可以在外部使用,这正是可以找到帮助的地方)

此外,替代 变量引用已得到增强。 您现在可以使用以下可选选项 语法:

%~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
可以组合这些修改器以获得 复合结果:

%~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
%~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
在上述示例中,可以使用%I和PATH 将被其他有效值替换。 语法以有效的 用于变量名。拾取大写字母 像%I这样的变量名使它更加 可读性强,避免与 修饰符,它们不是大小写 敏感


您可以使用不同的字母,如
f
表示“完整路径名”,
d
表示驱动器号,
p
表示路径,并且可以将它们组合在一起<代码>%~是这些序列中每个序列的开始,数字
I
表示它对参数
%I
起作用(其中
%0
是批处理文件的完整名称,就像您假设的那样)。

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

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

Help=

(首先,我想为batch推荐这个有用的参考站点: )

然后是另一个有用的解释:

dp0变量 在Windows中引用时,
%~dp0
(这是一个零)变量 批处理文件将扩展到该批处理的drive字母和path 文件

变量
%0-%9
引用批处理的命令行参数 文件<代码>%1-%9引用批处理文件名后的命令行参数。
%0
引用批处理文件本身

如果在百分比字符(
%
)后面加上波浪号字符(
~
), 您可以在参数编号之前插入修改器以更改 变量展开的方式。
d
修改器扩展到驱动器 字母和
p
修饰符扩展到参数的路径

示例:假设您在
C:
上有一个名为
bat_files
的目录,并且 该目录中有一个名为
example.bat
的文件。在这种情况下,
%~dp0
(组合
d
p
修饰符)将扩展到
C:\bat\u文件\

查看完整的解释

还有,看看

以及以下更明确的参考:

  • %cmdline%
    将返回传递给CMD.EXE的整个命令行

  • %*
    将返回从第一个命令行参数开始的命令行剩余部分(在Windows NT 4中,%*还包括所有前导空格)

  • %~dn
    将返回%n的驱动器号(n的范围从0到9),如果%n是有效的路径或文件名(无UNC)

  • %~pn
    将返回%n的目录,如果%n是有效的路径或文件名(无UNC)

  • %~nn
    将仅返回%n的文件名(如果%n是有效的文件名)

  • %~xn
    仅在%n是有效文件名的情况下返回文件扩展名%n

  • 如果%n是有效的文件名或目录,
    %~fn
    将返回%n的完全限定路径

加1 刚刚为神秘的
~
tilde操作符找到了一些很好的参考

%~
字符串称为运算符。您可以在以下情况下找到它:
%~0

:~
字符串称为运算符。您可以像
%SOME\u VAR:~0,-1%
一样找到它

新增2018年7月6日下午2点至1点12分
%1-%9
请参阅命令行参数。如果它们不是有效的路径值,
%~dp1
-
%~dp9
将全部扩展到与
%~dp0
相同的值。但是如果它们是有效的路径值,它们将扩展到自己的驱动程序/路径值

例如: (batch.bat)

运行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\

举个例子就好了——这里有一个小例子

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

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

或者,对于#OneLinerOvers,正如@Omni在评论中指出的那样,
cd/d%~dp0
将同时更改驱动器和目录:)


希望这对某人有所帮助。

草莓Perl便携式shell launcher的一个很好的例子:

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在那里做什么,但它很管用

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

为了获得清晰的理解,让我们在目录中创建一个批处理文件

C:\script\test.b