Welcome Guest! Web Capture Tool demo Log In | Sign Up!| Help|
RSS Feed RSS><
0 ThumbsUp
0 Comments
Title: Get Directory Path of an executing Batch file: %dp0 variable
Source: http://weblogs.asp.net/. . ./get-directory-path-of-an-executing-batch-file.aspx
 Shared by: Anonymous
In eFolders: Software Development


If "Get Directory Path of an executing Batch file: %dp0 variable" is not shown property. Visit the source link above.

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
 

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

Capture pictures, videos, ...anything like above with our Live Capture Tool for sharing and archiving. Watch the DEMO
Twitter This
FaceBook Link
StumbleUpon
Reddit
MORE »

ETAGLIVE: YOUR TALKBACKS
Comments for: Get Directory Path of an executing Batch file: %dp0 variable

To post comments, please Click Here to sign in
Contact Us | About us | Help | Privacy | Term of Service.  2007 eTagLive.com (TM), All rights reserved