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

7

1263

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 ~ doing? Does dp mean drive and path? Does the 0 refer to %0, the path to the batch file that includes the file name?

Or it is just a weird label?

I'd also like to know if it is a documented feature, or something prone to be deprecated.

Colicweed answered 17/2, 2011 at 20:15 Comment(6)
possible duplicate of What does %~d0 mean in a Windows batch file?Epispastic
You are right, Hellen. That question didn't show up when I searched for %~dp0. Maybe because it has %~d0 on it's title instead of %~dp0.Leukocyte
SymbolHound is great for conducting searches on other than alphanumeric characters. For instance, search for %~dp0. Usually I find that the top hits refer to content on SO.Tyndall
SymbolHound did not work for me with the search symbolhound.com/?q=%25~dp0. However this worked: google.com/search?q=%25~dp0 - just sayin'...Piecedyed
SymbolHound 2019: Does not work and uMatrix indicates that it connects to third party sites like blockchain.info and paypalobjects.com.Boric
@Epispastic I love that that answer, which is older, has a comment linking back to this one. ♾Memorize
P
958

Calling

for /?

in the command-line gives help about this syntax (which can be used outside FOR, too, this is just the place where help can be found).

In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

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

The modifiers can be combined to get compound results:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid FOR variable name. Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.

There are different letters you can use like f for "full path name", d for drive letter, p for path, and they can be combined. %~ is the beginning for each of those sequences and a number I denotes it works on the parameter %I (where %0 is the complete name of the batch file, just like you assumed).

Podgorica answered 17/2, 2011 at 20:19 Comment(5)
I don't know about anyone else, but I get "The following usage of the path operator in batch-parameter substitution is invalid. For valid formats type CALL /? or FOR /?. The syntax of the command is incorrect." when using I. I have to use 0 instead of I. I don't know if I've missed something or whatever, but just letting people know.Prenatal
@BenHooper, I is a placeholder for the variable index. 0 = the calling file, 1 = argument #1, 2 = argument #2, etc...Gunnery
This answer is good. And exhaustive. For a bit more brevity, I like the answer to this near-duplicate-question: stackoverflow.com/questions/112055/…Aldo
This answer is a little confusing because these substitutions can be used outside of FOR.Saideman
Note that if you run a batch file directly from C# using Process.Start, that batch file will expand %~dp0 as the working directory of the C# program and NOT the location of the batch file. If you use Process.Start to run cmd.exe which in turn runs the batch file (e.g. using arguments /C foo.bat) then all is well. I suspect this is to do with how and where PATH is expanded. You can set the WorkingDirectory of the Process to fix this problem.Lemieux
C
492

(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\
Croon answered 24/4, 2012 at 1:51 Comment(4)
Also curious about this concept and found the good clarification here. Thanks Mingge. ^_^Garlan
for me it expands to c:\bat_files\ (with trailing path seperator)Suburbanize
The links to the Microsoft article and the forum thread do not work anymore :(Callipygian
If you give it relative paths, they will be expanded as relative paths, which means relative to your current directory... which, in your test, happens to be the same directory as %~dp0. %~dp1 doesn't magically become %~dp0 just because the file arg1 doesn't exist; you might want your batch file to create it!Disorient
A
176

The variable %0 in a batch script is set to the name of the executing batch file.

The ~dp special syntax between the % and the 0 basically says to expand the variable %0 to show the drive letter and path, which gives you the current directory containing the batch file!

Help = Link

Asomatous answered 17/2, 2011 at 20:28 Comment(2)
@Andy, Is there any way to test %0 directly in the cmd without saving a .bat file to local harddrive?Bludge
Link is broken, and I am not able to find the correct link. A pending edit still has the broken link.Sara
Q
61

%~dp0 expands to current directory path of the running batch file.

To get clear understanding, let's create a batch file in a directory.

C:\script\test.bat

with contents:

@echo off
echo %~dp0

When you run it from command prompt, you will see this result:

C:\script\

Quemoy answered 25/11, 2014 at 8:44 Comment(0)
D
44

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.

Alternatively, for #oneLinerLovers, as @Omni pointed out in the comments cd /d %~dp0 will change both the drive and directory :)

Hope this helps someone.

Dayton answered 14/9, 2012 at 8:54 Comment(3)
cd has a built in parameter that will switch both the drive and the folder: cd /d %~dp0Riposte
You are right and the /d argument is helpful, but it deserves to be posted as a candidate alternative answer and not a comment to this one. Thank you for pointing this out though, I didn't know about it :)Dayton
you could also use ''pushd %~dp0'' to switch drive and folderRann
M
27

Great example from Strawberry Perl's portable shell launcher:

set drive=%~dp0
set drivep=%drive%
if #%drive:~-1%# == #\# set drivep=%drive:~0,-1%

set PATH=%drivep%\perl\site\bin;%drivep%\perl\bin;%drivep%\c\bin;%PATH%

not sure what the negative 1's doing there myself, but it works a treat!

Milan answered 25/9, 2012 at 18:39 Comment(2)
+1 very useful. The if statement checks if a backslash is present and removes the backslash if necessary. The negative 1 in the condition extracts the last character, and the expression in the set statement takes the path string minus the last character (which is the backslash).Mariejeanne
negative 1 is minus the last character. Read variable substringHalfprice
S
13

An example would be nice - here's a trivial one

for %I in (*.*) do @echo %~xI

it lists only the EXTENSIONS of each file in current folder

for more useful variable combinations (also listed in previous response) from the CMD prompt execute: HELP FOR which contains this snippet

The modifiers can be combined to get compound results:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line
Sorcery answered 21/6, 2012 at 16:31 Comment(3)
i got "~xI was unexpected at this time." trying out the example - is it because i am using Win 7?Veats
This is good information, but it fails to address the original post.Apollus
@Veats ~xI or ~xl or L / Low = l or I / Low = i ? 😲Superpatriot

© 2022 - 2024 — McMap. All rights reserved.