Get Directory Path of an executing Batch file

Most people probably know that can use the variable %0 in a batch file to get the name of the executing batch file. However if you use that in conjunction with some batch parameter modifiers you can easily split that into a drive, directory or filename. Therefore to get the location of an executing batch file from within that batch file you can use the variable %~dp0. Where d is the drive, p is the path and 0 is of course the name of the executing batch file.

This comes in real handy for me because I have some batch files on network drives that do some simple installs. Since the install files are usually in the same directory as batch file I can use %~dp0 as their path. Now when I double click on a batch file in windows explorer whether the drive is mapped or a UNC path the batch file has the correct path to the files.

If the drive is mapped this is not really necessary because the working directory is set to the directory that the batch file is in. However if you access this directory via a UNC path this is not the case. So by using %~dp0 you can get the correct directory path, even for UNC paths. Before I took the time to figure this out I always had to map the network drive to run the batch file, but no longer.

This was inspired by comments on Raymond's Capturing the current directory from a batch file post. On commenter suggested changing to the directory by using "cd /d %0\.." this of course doesn't work for UNC paths, so I just used the path instead. On another note instead of using "cd /d %0\.." to change the directory you can use "cd /d %~dp0" instead.

Enjoy!

Published Friday, January 28, 2005 4:36 PM by puzzlehacker

Comments

# re: Get Directory Path of an executing Batch file

To change to the current directory of a network location (or a local path), try using the following command: pushd "%~dp0" When you want to change back, just be sure to run "popd" at the end of your script to put the path back.

Tuesday, September 12, 2006 9:39 AM by yhamade

# re: Get Directory Path of an executing Batch file

your %~dp0 idea really helped me

thanks

Friday, December 8, 2006 4:30 AM by jokop

# re: Get Directory Path of an executing Batch file

Excellent post.  I've always wondered how to parse the %0 parameter thingy... now I don't have to include an environment variable %workingDirectory% in every batch file that I run.

Thanks!

Monday, January 8, 2007 7:06 PM by Blake

# re: Get Directory Path of an executing Batch file

The "batch parameter modifiers" link is dead...

Tuesday, April 17, 2007 3:30 PM by Jim

# re: Get Directory Path of an executing Batch file

Thanks for the tip!!!!

Friday, April 20, 2007 1:21 PM by Karl

# re: Get Directory Path of an executing Batch file

I updated the link... thanks Jim.

Sunday, April 22, 2007 3:19 PM by puzzlehacker

# re: Get Directory Path of an executing Batch file

thanks!!!

your %~dp0 idea really helped me!!!!!

Monday, April 30, 2007 7:58 AM by roman

# re: Get Directory Path of an executing Batch file

thanks for %~dp0 command......

this is helping me in many of my scripts

Wednesday, May 2, 2007 9:32 AM by kris

# re: Get Directory Path of an executing Batch file

Thx for this very nice article... It helps me a lot...

Sunday, June 3, 2007 11:56 PM by Andre

# re: Get Directory Path of an executing Batch file

On Windows XP and Vista you may use %CD% (for current directory)

Friday, June 8, 2007 1:33 AM by Michael Prescott

# re: Get Directory Path of an executing Batch file

Yes you can use %CD% assuming you are running that batch file from that directory. But I don't believe that will work if say double click it from an explorer folder.

Friday, June 8, 2007 10:49 PM by puzzlehacker

# re: Get Directory Path of an executing Batch file

Ultimate post !!

really helped me a lot ..

Hey do u have any more valuable info on environment variables, for loops.

I know basics of these but I want to use them to apply a bit complex logic.

tnx

Thursday, June 14, 2007 5:35 AM by sAc

# re: Get Directory Path of an executing Batch file

It is really helpful..it worked like a charm..thanks

Wednesday, August 1, 2007 3:55 AM by cooolvick@yahoo.com

# re: Get Directory Path of an executing Batch file

So useful. than you

Tuesday, October 9, 2007 11:12 AM by Otto

# re: Get Directory Path of an executing Batch file

With Windows 2000, I've seen Python and some other command-line applications and compilers fail to detect the directory path from which they are executing; however, the %CD% always works for me with Windows XP and Vista.  It's really easy to test to see if it works for you.  Create a batch file called echo_CD.bat and put the following statements in it:

echo %CD%

pause

Now, move the file into a few directories and double-click or open a command prompt.  Works either way for me.  Does it fail for anyone else?  Any ideas why it fails for puzzlehacker?

Wednesday, October 10, 2007 3:09 PM by Michael Prescott

# re: Get Directory Path of an executing Batch file

Thanks  it rely helps

Thursday, October 11, 2007 4:41 AM by Amr301

# re: Get Directory Path of an executing Batch file

%CD% will only work locally, whereas %~dp0 will work locally and over a network as well.  

Reason being Universal Naming Convention (UNC) paths are not supported for %CD%.  E.g., \\server\share\file_path

Bottom line: always use %~dp0 because it's more flexible.

Thursday, November 15, 2007 2:09 PM by sophophobe

# re: Get Directory Path of an executing Batch file

Cheers Mate! Exactly what I've been looking for! Thanks

Tuesday, November 20, 2007 4:17 PM by at0m

# re: Get Directory Path of an executing Batch file

Hey thats superb.. this is exactly what I was looking for.. gr8 work.. thanks pal..

Wednesday, December 5, 2007 5:01 AM by Suvrajit Dhar

# re: Get Directory Path of an executing Batch file

I had issues with %CD% when calling a couple batch files I had in other directories from a master batch file. It returned the wrong value ( the user directory for my logon ).

Your solution works beautifully and is far less "over engineered" than the environment variable ones I saw.

Thanks a lot!

Wednesday, January 9, 2008 2:30 PM by CrashTest

# re: Get Directory Path of an executing Batch file

Thanks ... (%~dp0) ...  a lot ..

Friday, March 14, 2008 12:43 PM by Eduardo

# re: Get Directory Path of an executing Batch file

Thanks for the tip! Also, I never could find a good source for 'supported' commands and such for batch files. I'll be implementing this in csbatch!

Wednesday, March 19, 2008 1:40 PM by Kody Brown

# re: Get Directory Path of an executing Batch file

awesome post,it really clear my confusion.

one more question here:

set _DEVROOT=%~dp0

set _DEVROOT=%_DEVROOT:~0,-21%

my question is what the _DEVROOT is now? what's the meaning of ~0,-21?

Thanks

Thursday, March 20, 2008 9:14 AM by neil

# re: Get Directory Path of an executing Batch file

Thanks. with a wide variety in network drives and paths this really helped automate my scripts

Friday, April 11, 2008 11:51 AM by Ryan

# re: Get Directory Path of an executing Batch file

Very cool... Thanks dude!

Wednesday, April 30, 2008 6:27 AM by wotamidoing

# re: Get Directory Path of an executing Batch file

Awesome!  Thanks for taking the time to post this info...

Tuesday, June 17, 2008 3:38 PM by Sneak

# re: Get Directory Path of an executing Batch file

Thanks for %~dp0!!!

Friday, July 18, 2008 7:46 AM by Carlo

# re: Get Directory Path of an executing Batch file

Great Post.  Saved me a great deal of time and frustration!

Friday, July 25, 2008 9:24 PM by EW

# re: Get Directory Path of an executing Batch file

Thanks a lot! This is a life saver!

Wednesday, August 13, 2008 2:39 PM by John Francis

# re: Get Directory Path of an executing Batch file

Great information! A big help.

Wednesday, August 13, 2008 11:09 PM by NG

# re: Get Directory Path of an executing Batch file

WOW GENIOUS!! Why the hell aren't there more guys like puzzlehacker!!

Wednesday, October 22, 2008 9:15 AM by chris

# re: Get Directory Path of an executing Batch file

this is really good. what if my batch file is in a zip. will i be able to get the path for my zip file. ?

Please help me

Tuesday, November 4, 2008 10:46 AM by Ravi

# re: Get Directory Path of an executing Batch file

You just made my day, man.

Thursday, November 6, 2008 12:07 PM by Najati

# re: Get Directory Path of an executing Batch file

Thanks for the nice post. It saved my day.

Tuesday, November 11, 2008 11:17 AM by VJAlla

# re: Get Directory Path of an executing Batch file

helped me a lot, thanks

i was looking for something this simple but didn't expect it really exist.

Friday, November 14, 2008 12:32 PM by Tim

# re: Get Directory Path of an executing Batch file

Oh man! you, awesome........

Wednesday, December 10, 2008 11:46 PM by Dung

# re: Get Directory Path of an executing Batch file

Oh yes!

Good good work!

Thursday, December 11, 2008 9:43 AM by Ed

# re: Get Directory Path of an executing Batch file

thanks buddy you solved my big problem thanks

Saturday, December 13, 2008 10:49 AM by rohit afterglow

# re: Get Directory Path of an executing Batch file

Please help... any assistance is greatly appreciated. ~Tom Cusick

I have a batch file on a server that is run from a scheduled process.  I want to create a dated directory on the server location to backup the data files.

When running the below code, I get an Error...

[CODE]

cd %~dp0  REM \\pa-file1\FTP-idpdw Directory the file is running from

for /F "tokens=2-4 delims=/- " %%A in ('date/T') do set var= %%A%%B%%C

md %var%

pause

xcopy %~dp0\*.* %~dp0\%var%\

REM xcopy \\pa-file1\FTP-idpdw\*.* \\pa-file1\FTP-idpdw\%var%\

[/CODE]

[ERROR]

'\\Pa-file1\ftp-idpdw'

CMD.EXE was started with the above path as the current directory.

UNC paths are not supported.  Defaulting to Windows directory.

'\\Pa-file1\ftp-idpdw\'

CMD does not support UNC paths as current directories.

[/ERROR]

Thursday, December 18, 2008 8:43 AM by Tom Cusick

# re: Get Directory Path of an executing Batch file

cd doesn't like UNC (i.e. network paths), either don't CD to the directory or use pushd for UNC directories instead.

Thursday, December 18, 2008 11:30 AM by puzzlehacker

# re: Get Directory Path of an executing Batch file

Thanks!  This is exactly what I was looking for.  Greetings from Taiwan!

Friday, February 20, 2009 5:26 AM by Jack

# re: Get Directory Path of an executing Batch file

This %d %p really helped me and was exactly what I was looking for thanks!

Thursday, April 16, 2009 12:42 PM by Guy

# re: Get Directory Path of an executing Batch file

Don't recall having this back in the MS 6.0 days.. :)

Thanks!

Sunday, May 3, 2009 5:52 PM by Avi

# re: Get Directory Path of an executing Batch file

thanks,

%~d0

cd %~p0

is much simpler than:

for %%s in (%0) do %%~ds

for %%s in (%0) do cd %%~ps

Friday, May 15, 2009 1:09 AM by xP

# re: Get Directory Path of an executing Batch file

Many thanks. It is extemely helpful.

Friday, May 29, 2009 1:04 PM by Lucy

# re: Get Directory Path of an executing Batch file

Thanks,didn't even know that theres such a thing as "batch parameter modifiers"

Monday, June 8, 2009 11:35 PM by Asela

# re: Get Directory Path of an executing Batch file

%~dp0 is kickass that helped me so much

Friday, June 26, 2009 9:23 AM by andrew

# re: Get Directory Path of an executing Batch file

Awesome dude - excellent snippet ... very usefull !

Friday, July 24, 2009 9:00 AM by Vildand

# re: Get Directory Path of an executing Batch file

Thanks a lot very useful!

Thursday, August 20, 2009 4:42 PM by David

# re: Get Directory Path of an executing Batch file

Well, 4.5 years later and your advice is still coming in handy. This solved a significant problem I was having. Thanks!

Tuesday, September 1, 2009 9:26 PM by Kelly

# re: Get Directory Path of an executing Batch file

This is wonderful!! I always had few lines of script to parse the Directory path from %0, but this never struck me.

Monday, October 5, 2009 5:28 PM by Anand

# re: Get Directory Path of an executing Batch file

Why did I not think of this before. This is awesome. Solves a lot of isues for me.

Monday, October 19, 2009 1:16 PM by Dhrumil Shah

# re: Get Directory Path of an executing Batch file

Thanx...

This is very helpful.

Saturday, October 31, 2009 10:23 AM by swagat

# re: Get Directory Path of an executing Batch file

Thanks for the article. Using %~dp0 solved my problem with UNC path.

Friday, November 6, 2009 6:39 PM by Naveen

# re: Get Directory Path of an executing Batch file

Almost 5 years later and this is still a very helpful piece of information. Thanks and Cheers!

Thursday, November 19, 2009 10:25 PM by Rudy Lattae

# re: Get Directory Path of an executing Batch file

Thanks a lot..

Its very helpful

Tuesday, December 29, 2009 10:56 AM by Vikrant

# re: Get Directory Path of an executing Batch file

thanks dudes help me alot...

Thursday, January 14, 2010 1:01 AM by bm

# re: Get Directory Path of an executing Batch file

thank you.

dows anyone knows how to open the

%~dp0..\..\whateverFolder

in the currently opened explorer instead of creating a new instance?

Thursday, May 6, 2010 3:25 AM by cloud

# re: Get Directory Path of an executing Batch file

If (like me) you're having trouble with %~dp0 with directory name including (for e.g.) an "&" replace :

cd %~dp0

with

cd %0\..

Friday, May 28, 2010 4:15 AM by Denis Villechalane

# re: Get Directory Path of an executing Batch file

this works too

    set path=%0\..

then use %path% in batch script

Tuesday, September 21, 2010 6:51 PM by debo

# re: Get Directory Path of an executing Batch file

This tip saved an hour a week for me, thanks.

Wednesday, October 20, 2010 12:29 PM by Jim

# re: Get Directory Path of an executing Batch file

This cd /d %~dp0  syntax just saved my professional derrier. Thanks very much!!!! I just inserted into 30+ bat files that stopped working with our application that was moved from Win XP to Win 7.

Thanks again!!!

jack

Friday, January 21, 2011 11:05 PM by jack

# re: Get Directory Path of an executing Batch file

>On Windows XP and Vista you may use %CD% (for current directory)

It worked for me until Windows 7, where when I run a batch, at least as Administrator, my path if "C:\Windows\System32"

cd %~dp0 solved thanks !

Tuesday, January 25, 2011 4:22 AM by bob

# re: Get Directory Path of an executing Batch file

Excellent tip, it can be used to find the your batch executing directory when running cmd.exe as administrator direct you to C:\system32

Thursday, February 17, 2011 11:45 PM by Lijian

# re: Get Directory Path of an executing Batch file

thanks a lot...%~dp0 helped me to fix the issue t my end..cheers guys...

Wednesday, July 20, 2011 10:30 AM by Dilip

# re: Get Directory Path of an executing Batch file

Thanks guy. You're guru of batch files...

Friday, April 27, 2012 5:08 AM by drweb86

# re: Get Directory Path of an executing Batch file

hello can some one help me on  this?

am using below script to search files with particular string..

@echo off

echo %CD%

dir /b|Find "ABC"

am not getting any results diplayed..

there are files with string "ABC"

Friday, June 15, 2012 9:53 AM by satish

# re: Get Directory Path of an executing Batch file

WOW, 7 1/2 years after the original post and today I really needed this answer. You have saved me tons of greif/time in my script automation in a windows 7 enviroment. Thanks man!!

Gotta love the Internet :)

Thursday, July 19, 2012 12:51 PM by John C