我在看一个批处理文件,它定义了以下变量:

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

%~d0或%~p0到底是什么意思? 是否有一组众所周知的值,如当前目录、驱动器、脚本参数? 还有其他类似的捷径可以用吗?


当前回答

是的,你还可以使用下面给出的其他快捷方式。 在您的命令中,~d0表示第0个参数的驱动器号。

~ expands the given variable
d gets the drive letter only
0 is the argument you are referencing

由于第0个参数是脚本路径,它为您获取路径的驱动器号。你也可以使用下面的快捷方式。

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 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    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line

这也可以直接在命令提示符中找到,当你运行CALL /?或FOR /?

其他回答

从批处理文件中的文件名解析和更多成语- Real的如何:

脚本所在的路径(不包含驱动器):~p0

脚本所在的驱动器:~d0

%~d0提供参数0的驱动器号(脚本名),%~p0提供路径。

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

因为参数通常是文件路径,所以有一些额外的语法来提取路径的部分。~d是驱动器,~p是路径(没有驱动器),~n是文件名。它们可以组合,所以~dp是驱动器+路径。

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

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

请在这里查找所有命令行命令的参考。波浪魔术代码在for下面描述。

它显示您当前所在的文件或目录的当前位置。例如;如果批处理文件在桌面目录中,那么“%~dp0”将显示桌面目录。如果你想显示当前目录和当前文件名,你可以输入“%~dp0%~n0%~x0”。

是的,你还可以使用下面给出的其他快捷方式。 在您的命令中,~d0表示第0个参数的驱动器号。

~ expands the given variable
d gets the drive letter only
0 is the argument you are referencing

由于第0个参数是脚本路径,它为您获取路径的驱动器号。你也可以使用下面的快捷方式。

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 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    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line

这也可以直接在命令提示符中找到,当你运行CALL /?或FOR /?