What does %~d0 mean in a Windows batch file?

WindowsBatch File

Windows Problem Overview


I'm looking at a batch file which defines the following variables:

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0
  • 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?

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

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 /?.

Solution 3 - Windows

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 /?

Solution 4 - Windows

From [Filename parsing in batch file and more idioms - Real's How-to](http://www.rgagnon.com/gp/gp-0008.html "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

Solution 5 - Windows

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.

Solution 6 - Windows

%~d0 gives you the drive letter of argument 0 (the script name), %~p0 the path.

Solution 7 - Windows

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
test

Same for tEsTEnter:

c:\>tEsT
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).

Solution 8 - Windows

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

Solution 9 - Windows

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".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris SmithView Question on Stackoverflow
Solution 1 - WindowsJacquesBView Answer on Stackoverflow
Solution 2 - WindowsefotinisView Answer on Stackoverflow
Solution 3 - WindowsClewaksView Answer on Stackoverflow
Solution 4 - WindowsWilliam KellerView Answer on Stackoverflow
Solution 5 - WindowsMarvin ThobejaneView Answer on Stackoverflow
Solution 6 - WindowsArmin RonacherView Answer on Stackoverflow
Solution 7 - WindowsPacerierView Answer on Stackoverflow
Solution 8 - WindowsdjangofanView Answer on Stackoverflow
Solution 9 - WindowszaskView Answer on Stackoverflow