What does %~dp0 mean, and how does it work?

后端 未结 7 1985

I find %~dp0 very useful, and I use it a lot to make my batch files more portable.

But the label itself seems very cryptic to me... What is the ~<

相关标签:
7条回答
  • 2020-11-22 03:05

    (First, I'd like to recommend this useful reference site for batch: http://ss64.com/nt/)

    Then just another useful explanation: http://htipe.wordpress.com/2008/10/09/the-dp0-variable/

    The %~dp0 Variable

    The %~dp0 (that’s a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file.

    The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.

    If you follow the percent character (%) with a tilde character (~), you can insert a modifier(s) before the parameter number to alter the way the variable is expanded. The d modifier expands to the drive letter and the p modifier expands to the path of the parameter.

    Example: Let’s say you have a directory on C: called bat_files, and in that directory is a file called example.bat. In this case, %~dp0 (combining the d and p modifiers) will expand to C:\bat_files.

    Check out this Microsoft article for a full explanation.

    Also, check out this forum thread.

    And a more clear reference from here:

    • %CmdCmdLine% will return the entire command line as passed to CMD.EXE

    • %* will return the remainder of the command line starting at the first command line argument (in Windows NT 4, %* also includes all leading spaces)

    • %~dn will return the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC)

    • %~pn will return the directory of %n if %n is a valid path or file name (no UNC)

    • %~nn will return the file name only of %n if %n is a valid file name

    • %~xn will return the file extension only of %n if %n is a valid file name

    • %~fn will return the fully qualified path of %n if %n is a valid file name or directory

    ADD 1

    Just found some good reference for the mysterious ~ tilde operator.

    The %~ string is called percent tilde operator. You can find it in situations like: %~0.

    The :~ string is called colon tilde operator. You can find it like %SOME_VAR:~0,-1%.

    ADD 2 - 1:12 PM 7/6/2018

    %1-%9 refer to the command line args. If they are not valid path values, %~dp1 - %~dp9 will all expand to the same value as %~dp0. But if they are valid path values, they will expand to their own driver/path value.

    For example: (batch.bat)

    @echo off
    @echo ~dp0= %~dp0
    @echo ~dp1= %~dp1
    @echo ~dp2= %~dp2
    @echo on
    

    Run 1:

    D:\Workbench>batch arg1 arg2
    
    ~dp0= D:\Workbench\
    ~dp1= D:\Workbench\
    ~dp2= D:\Workbench\
    

    Run 2:

    D:\Workbench>batch c:\123\a.exe e:\abc\b.exe
    
    ~dp0= D:\Workbench\
    ~dp1= c:\123\
    ~dp2= e:\abc\
    
    0 讨论(0)
提交回复
热议问题