Suppose i have a .bat file saved in a folder .
When my .bat file runs i need to get the path in which this .bat file is saved . I tried Google it but could not find the answer.
Thanks.
Asked
Active
Viewed 171 times
1

osman malik
- 17
- 5
-
Duplicate of [What does %~dp0 mean, and how does it work?](http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work) which is second on [batch-file votes](http://stackoverflow.com/questions/tagged/batch-file?sort=votes&pageSize=30) list. Not so famous is [In Batch file ~dp0 changes on changing directory](http://stackoverflow.com/questions/12141482/) – Mofi Dec 23 '14 at 13:59
1 Answers
0
%~dp0
will give you teh path to the bat file.
If you also need the whole path including the name of the bat file itself use %~0
.

MichaelS
- 5,941
- 6
- 31
- 46
-
-
No. %~dp0 is always available in any bat file. Don't set it, just use it! :) – MichaelS Dec 23 '14 at 12:12
-
-
Say your bat file is in C:\TEST\BATFOLDER\BatFile.bat. Put the line `@ECHO %~dp0` into your bat and it will print `C:\TEST\BATFOLDER\\`. – MichaelS Dec 23 '14 at 12:18
-
-
Sure. `Set MyVar=%~dp0` will work. Afterwards you can access it with `%MyVar%`. But why would you put it into a variable if you can always access it with `%~dp0` instead? However, it is possible. – MichaelS Dec 23 '14 at 12:24
-