# 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!