Windows批处理文件中的%〜d0是什么意思?

时间:2020-03-06 14:30:54  来源:igfitidea点击:

我正在查看定义以下变量的批处理文件:

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0
  • %〜d0或者%〜p0到底是什么意思?
  • 是否存在一组诸如当前目录,驱动器,脚本参数之类的知名值?
  • 我还有其他类似的快捷方式可以使用吗?

解决方案

从批处理文件中的Filename解析和更多惯用语Real的操作方法:

脚本所在的路径(无驱动器):〜p0

脚本所在的驱动器:〜d0

"%〜d0"为我们提供参数0的驱动器号(脚本名称),而"%〜p0"为路径。

它们是增强的变量替换。它们修改批处理文件中使用的%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 /?找到以上内容。

魔术变量n包含用于调用文件的参数:%0是蝙蝠文件本身的路径,%1是后面的第一个参数,%2是第二个,依此类推。 。

由于参数通常是文件路径,因此有一些其他语法可提取部分路径。 〜d是驱动器,〜p是路径(没有驱动器),〜n是文件名。它们可以组合在一起,所以〜dp是驱动器+路径。

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

我们还可以获得有关文件的其他类型的元信息:〜t是时间戳,〜z是大小。

在此处查找所有命令行命令的参考。波浪符代码在for下描述。