【问题标题】:What does %~d0 mean in a Windows batch file?Windows 批处理文件中的 %~d0 是什么意思?
【发布时间】:2010-09-11 20:11:28
【问题描述】:

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

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0
  • %~d0%~p0 究竟是什么意思?
  • 当前目录、驱动器、脚本参数等是否有一组众所周知的值?
  • 我可以使用其他类似的快捷方式吗?

【问题讨论】:

标签: windows batch-file


【解决方案1】:

这段代码解释了 ~tilde 字符的使用,这对我来说是最令人困惑的事情。一旦我理解了这一点,它就会使事情变得更容易理解:

@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

【讨论】:

    【解决方案2】:

    来自Filename parsing in batch file and more idioms - Real's How-to

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

    脚本所在的驱动器:~d0

    【讨论】:

    • 关于 ~[idiom]s 的很棒的链接和信息。还解释了完整的脚本名称%~s0
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      它显示您当前所在的文件或目录的当前位置。例如;如果您的批处理文件在桌面目录中,则 "%~dp0" 将显示桌面目录。如果您希望它以当前文件名显示当前目录,您可以键入“%~dp0%~n0%~x0”。

      【讨论】:

      • "%~dpnx0" 会更好
      【解决方案5】:

      需要注意的一些问题:

      如果您双击批处理文件%0 将被引号括起来。例如,如果您将此文件保存为c:\test.bat

      @echo %0
      @pause
      

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

      "C:\test.bat"
      

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

      c:\>test.bat
      test.bat
      

      如果你输入testEnter%0 的输出也将没有扩展名,因为你没有输入扩展名:

      c:\>test
      test
      

      tEsT也一样输入

      c:\>tEsT
      tEsT
      

      如果您输入"test"Enter%0 的输出将包含引号(因为您输入了引号)但没有扩展名:

      c:\>"test"
      "test"
      

      最后,如果您输入"C:\test.bat",输出将与您双击它时完全一样:

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

      请注意,%0 并非所有可能的值,因为您可以从其他文件夹调用脚本:

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

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

      【讨论】:

        【解决方案6】:

        另一个很有帮助的提示是将当前目录设置为不同的驱动器,必须先使用 %~d0,然后再使用 @ 987654322@。这会将目录更改为批处理文件的驱动器,然后更改为它的文件夹。

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

        希望这对某人有所帮助。

        【讨论】:

        • 另一个选项是pushd %~dp0,它可以毫无问题地更改当前驱动器,并为您提供额外的好处,即以后能够popd 回到原来的驱动器/路径。
        【解决方案7】:

        是的,您可以使用下面给出的其他快捷方式。 在您的命令中, ~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 /?

        【讨论】:

          【解决方案8】:

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

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

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

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

          查看here 以获取所有命令行命令的参考。波浪号魔术代码在for 下进行了描述。

          【讨论】:

          • 自我注意:echo %~dp0 不能在命令行中使用,只能在批处理文件中使用。呵呵!
          • %~d0%~d0%有区别吗?
          • @Pacerier: %0 是包含脚本文件名的完整路径。 %~dp0 是包含脚本的文件夹的路径,但不包括脚本的文件名。
          • 自我说明:echo %~pd0 给出与 echo %~dp0 相同的输出(而不是预期的相反)。此外,%0 的值是different depending on whether you double-click 批处理文件或从 cmd 运行。
          【解决方案9】:

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

          【讨论】:

            猜你喜欢
            • 2013-09-23
            • 1970-01-01
            • 2012-01-19
            • 2013-03-11
            • 1970-01-01
            • 1970-01-01
            • 2013-05-14
            • 1970-01-01
            相关资源
            最近更新 更多