What does %~d0 mean in a Windows batch file?
我正在查看定义以下变量的批处理文件:
1 2 | set _SCRIPT_DRIVE=%~d0 set _SCRIPT_PATH=%~p0 |
-
%~d0 或%~p0 实际上意味着什么? - 对于诸如当前目录,驱动器,脚本参数之类的东西,是否有一组众所周知的值?
- 我可以使用其他类似的快捷方式吗?
神奇变量
由于参数通常是文件路径,因此有一些额外的语法来提取路径的一部分。
因此,
您还可以获取有关该文件的其他类型的元信息:
在此处查看所有命令行命令的参考。代字号为魔术代码。
它们是增强的可变取代。它们修改批处理文件中使用的%N个变量。如果你在Windows中进行批量编程,那将非常有用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | %~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 |
您可以通过运行
是的,您可以使用下面给出的其他快捷方式。
在你的命令中,~d0意味着第0个参数的驱动器号。
1 2 3 | ~ expands the given variable d gets the drive letter only 0 is the argument you are referencing |
由于第0个参数是脚本路径,因此它会为您获取路径的驱动器号。您也可以使用以下快捷方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | %~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
另一个有用的提示是,要将当前目录设置为不同的驱动器,必须先使用
对于#oneLinerLovers,
希望这有助于某人。
一些值得注意的问题:
如果双击批处理文件,
1 2 | @echo %0 @pause |
双击它将打开一个带有输出的新命令提示符:
1 | "C:\test.bat" |
但是,如果您首先打开命令提示符并直接从该命令提示符调用它,
1 2 | c:\>test.bat test.bat |
如果您键入
1 2 | c:\>test test |
对于
1 2 | c:\>test test |
如果键入
1 2 | c:\>"test" "test" |
最后,如果您键入
1 2 | c:\>"C:\test.bat" "C:\test.bat" |
请注意,这些不是
1 2 | c:\some_folder>/../teST.bAt /../teST.bAt |
上面显示的所有示例也会影响
这段代码解释了?tilda字符的使用,这对我来说是最令人困惑的事情。一旦我理解了这一点,它就会让事情变得更容易理解:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @ECHO off SET"PATH=%~dp0;%PATH%" ECHO %PATH% ECHO. CALL :testargs"these are days""when the brave endure" GOTO :pauseit :testargs SET ARGS=%~1;%~2;%1;%2 ECHO %ARGS% ECHO. exit /B 0 :pauseit pause |
它显示您当前所在的文件或目录的当前位置。如果批处理文件位于桌面目录中,则"%~dp0"将显示桌面目录。如果您希望它以当前文件名显示当前目录,则可以键入"%~dp0%~n0%~x0"。