添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
留胡子的煎饼  ·  R可视乎 | ...·  11 月前    · 
暗恋学妹的煎饼  ·  单引号 - 知乎·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
  • What do %~d0 or %~p0 actually mean?
  • Is there a set of well-known values for things like current directory, drive, parameters to a script?
  • Are there any other similar shortcuts I could use?
  • The magic variables % n contains the arguments used to invoke the file: %0 is the path to the bat-file itself, %1 is the first argument after, %2 is the second and so on.

    Since the arguments are often file paths, there is some additional syntax to extract parts of the path. ~d is drive, ~p is the path (without drive), ~n is the file name. They can be combined so ~dp is drive+path.

    %~dp0 is therefore pretty useful in a bat: it is the folder in which the executing bat file resides.

    You can also get other kinds of meta info about the file: ~t is the timestamp, ~z is the size.

    Look here for a reference for all command line commands. The tilde-magic codes are described under for .

    @Pacerier: %0 is the full path including the filename of the script. %~dp0 is the path to the folder containing the script but excluding the filename of the script. JacquesB Jul 15, 2015 at 19:06 note to self: echo %~pd0 gives the same output as echo %~dp0 (instead of inverse as expected). Also, the value of %0 is different depending on whether you double-click the batch file or run it from cmd. Pacerier Aug 11, 2015 at 12:09

    They are enhanced variable substitutions. They modify the %N variables used in batch files. Quite useful if you're into batch programming in 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
    

    You can find the above by running FOR /?.

    This online documentation is very confusing. I have never once met a programmer who knew to look in for /? docs for a docs about a variable used outside a for loop! Plus, it's unclear (to me and many others) that expands %I means "you can replace I with 0". Regardless, this answer is still useful to explain to others where the original docs can be found. – kevinarpe Feb 21 at 7:40

    Yes, There are other shortcuts that you can use which are given below. In your command, ~d0 would mean the drive letter of the 0th argument.

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

    As the 0th argument is the script path, it gets the drive letter of the path for you. You can use the following shortcuts too.

    %~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
    

    This can be also found directly in command prompt when you run CALL /? or FOR /?

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

    The path (without drive) where the script is : ~p0

    The drive where the script is : ~d0

    Another tip that would help a lot is that to set the current directory to a different drive one would have to use %~d0 first, then cd %~dp0. This will change the directory to the batch file's drive, then change to its folder.

    For #oneLinerLovers, cd /d %~dp0 will change both the drive and directory :)

    Hope this helps someone.

    another option is pushd %~dp0, which has no problem changing the current drive, and gives you the added bonus of being able to popd back to the original drive/path later. – bcrist Mar 13, 2016 at 1:48

    Some gotchas to watch out for:

    If you double-click the batch file %0 will be surrounded by quotes. For example, if you save this file as c:\test.bat:

    @echo %0
    @pause
    

    Double-clicking it will open a new command prompt with output:

    "C:\test.bat"
    

    But if you first open a command prompt and call it directly from that command prompt, %0 will refer to whatever you've typed. If you type test.batEnter, the output of %0 will have no quotes because you typed no quotes:

    c:\>test.bat
    test.bat
    

    If you type testEnter, the output of %0 will have no extension too, because you typed no extension:

    c:\>test
    

    Same for tEsTEnter:

    c:\>tEsT
    

    If you type "test"Enter, the output of %0 will have quotes (since you typed them) but no extension:

    c:\>"test"
    "test"
    

    Lastly, if you type "C:\test.bat", the output would be exactly as though you've double clicked it:

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

    Note that these are not all the possible values %0 can be because you can call the script from other folders:

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

    All the examples shown above will also affect %~0, because the output of %~0 is simply the output of %0 minus quotes (if any).

    This code explains the use of the ~tilde character, which was the most confusing thing to me. Once I understood this, it makes things much easier to understand:

    @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
    

    It displays the current location of the file or directory that you are currently in. for example; if your batch file was in the desktop directory, then "%~dp0" would display the desktop directory. if you wanted it to display the current directory with the current file name you could type "%~dp0%~n0%~x0".

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.