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 08, 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 08, 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 02, 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 03, 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 08, 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 08, 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 01, 2007 3:55 AM by cooolvick@yahoo.com

# re: Get Directory Path of an executing Batch file

So useful. than you

Tuesday, October 09, 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 05, 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 09, 2008 2:30 PM by CrashTest

# re: Get Directory Path of an executing Batch file

The MS document he was referring to is here now: technet2.microsoft.com/.../97731e49-ffa3-4918-87fb-5318743f29321033.mspx

Tuesday, February 19, 2008 8:35 PM by Nico57

# 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

# Scheduling tasks via a batch file: where am I?

One of the things that comes with configuring software for a controlled environment is that every single

Monday, June 09, 2008 9:43 AM by Cluebat-man to the rescue

# 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

Leave a Comment

(required) 
(required) 
(optional)
(required)