代码之家  ›  专栏  ›  技术社区  ›  Chris Smith

在Windows批处理文件中,%~d0是什么意思?

  •  313
  • Chris Smith  · 技术社区  · 16 年前

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

    set _SCRIPT_DRIVE=%~d0
    set _SCRIPT_PATH=%~p0
    
    • 做什么 %~d0 %~p0 实际上是什么意思?
    • 对于当前目录、驱动器、脚本参数等,是否有一组众所周知的值?
    • 我可以使用其他类似的快捷方式吗?
    9 回复  |  直到 7 年前
        1
  •  470
  •   JacquesB    16 年前

    魔法变量 % n 包含用于调用文件的参数: %0 是指向BAT文件本身的路径, %1 是之后的第一个争论, %2 是第二个等等。

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

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

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

    here 作为所有命令行命令的参考。tilde magic代码描述如下 for .

        2
  •  134
  •   Cristian Ciupitu    7 年前

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

        3
  •  44
  •   Clewaks    12 年前

    是的,您可以使用下面给出的其他快捷方式。 在您的命令中,~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/时直接在命令提示符中找到。或者?

        4
  •  9
  •   Wolf    7 年前

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

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

    脚本所在的驱动器:~d0

        5
  •  8
  •   Marvin Thobejane    10 年前

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

    对于一条线路, cd /d %~dp0 将同时更改驱动器和目录:)

    希望这能帮助别人。

        6
  •  6
  •   Armin Ronacher    16 年前

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

        7
  •  4
  •   Pacerier    9 年前

    有些人需要注意:

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

    @echo %0
    @pause
    

    双击将打开一个新的命令提示,并输出:

    "C:\test.bat"
    

    但是,如果您首先打开一个命令提示符并直接从该命令提示符调用它, % 0 会提到你所拥有的一切 类型化的 . 如果你打字 test.bat 进入 输出 % 0 将没有引号,因为您没有键入引号:

    c:\>test.bat
    test.bat
    

    如果你打字 test 进入 输出 % 0 也将没有扩展名,因为您没有键入扩展名:

    c:\>test
    test
    

    同样 tEsT 进入 :

    c:\>tEsT
    tEsT
    

    如果你打字 "test" 进入 输出 % 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 减去引号(如果有)。

        8
  •  2
  •   djangofan    12 年前

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

    @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
    
        9
  •  0
  •   zask    9 年前

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