在Windowsbatch file中,%〜d0是什么意思?

我正在查看一个batch file,它定义了以下variables:

set _SCRIPT_DRIVE=%~d0 set _SCRIPT_PATH=%~p0 
  • %~d0%~p0究竟是什么意思?
  • 是否有一些众所周知的值,如当前目录,驱动器,脚本参数?
  • 还有其他类似的捷径可以使用吗?

魔术variables% n包含用来调用文件的参数: %0是bat文件本身的path, %1是第二个参数, %2是第二个参数,依此类推。

由于参数通常是文件path,因此还有一些额外的语法来提取部分path。 ~d是驱动器, ~p是path(无驱动器), ~n是文件名。 他们可以合并,所以~dp是驱动器+path。

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

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

在这里寻找所有命令行命令的参考。 代字魔法代码在下面描述。

他们是增强的variables替代。 它们修改batch file中使用的%N个variables。 非常有用,如果你在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 /?”中find上述文件。

是的,还有其他的捷径,你可以使用下面给出的。 在你的命令中,〜d0意味着第0个参数的驱动器号。

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

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

 %~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 /时,这也可以直接在命令提示符中find。 或FOR /?

从这里 :

脚本所在的path(不带驱动器):〜p0

脚本所在的驱动器:〜d0

另一个有用的技巧是将当前目录设置为不同的驱动器 ,首先使用%~d0 cd %~dp0 ,然后cd %~dp0 。 这会将目录更改为batch file的驱动器,然后更改为其文件夹。

对于#oneLinerLovers, cd /d %~dp0将改变驱动器和目录:)

希望这有助于某人。

%~d0给出参数0(脚本名)的驱动器号, %~p0是path。

这个代码解释了〜tildaangular色的使用,这是我最困惑的事情。 一旦我明白了这一点,就会使事情变得容易理解:

 @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 

有些小问题需要注意:

如果双击batch file%0将被引号包围。 例如,如果将此文件保存为c:\test.bat

 @echo %0 @pause 

双击它将打开一个带有输出的新命令提示符:

 "C:\test.bat" 

但是,如果您首先打开命令提示符并直接从该命令提示符调用,则%0将引用您input的任何内容 。 如果您键入test.bat 回车%0的输出将没有引号,因为您键入了不引号:

 c:\>test.bat test.bat 

如果inputtest ,则%0的输出也不会有扩展名,因为您没有input扩展名:

 c:\>test test 

相同的input

 c:\>tEsT tEsT 

如果您input"test" Enter ,则%0的输出将会有引号(因为您键入了它们),但是没有扩展名:

 c:\>"test" "test" 

最后,如果你input"C:\test.bat" ,输出结果就像你双击它一样:

 c:\>"C:\test.bat" "C:\test.bat" 

请注意,这些不是所有可能的值%0都可以,因为您可以从其他文件夹调用脚本:

 c:\some_folder>/../teST.bAt /../teST.bAt 

上面所示的所有示例也会影响%~0 ,因为%~0 〜0的输出只是%0减去引号的输出(如果有的话)。

它显示您当前所在文件或目录的当前位置。 如果您的batch file位于桌面目录中,则“%〜dp0”将显示桌面目录。 如果你想用当前文件名显示当前目录,你可以input“%〜dp0%〜n0%〜x0”。