在Windows批处理文件中,%~d0代表什么意思?

10 浏览
0 Comments

在Windows批处理文件中,%~d0代表什么意思?

我正在查看一个批处理文件,其中定义了以下变量:

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0

  • %~d0%~p0 实际上是什么意思?
  • 是否有一组众所周知的值,例如当前目录、驱动器、脚本参数?
  • 是否还有其他类似的快捷方式可供使用?
admin 更改状态以发布 2023年5月22日
0
0 Comments

它们是增强的变量替换。它们修改批处理文件中使用的%N变量。如果您正在Windows中进行批处理编程,那么非常有用。

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

您可以通过运行FOR /?来找到上面提到的内容。

0
0 Comments

%n魔法变量包含用于调用文件的参数:%0是批处理文件本身的路径,%1是其后的第一个参数,%2是第二个参数,以此类推。

由于参数通常是文件路径,因此有一些额外的语法以提取路径的部分。 ~d是驱动器,~p是路径(不包括驱动器),~n是文件名。它们可以组合在一起,因此~dp是驱动器+路径。

%~dp0在bat文件中非常有用:它是正在执行的bat文件所在的文件夹。

您还可以获取有关文件的其他种类的元信息:~ t是时间戳,~z是大小。

请参见此处以获得所有命令行命令的参考。在for下描述了波浪线魔术代码。

0