How to get the path of the currently running batch job in MS-DOS?
I have a batch foo.bar
in a directory d:\progs\
.
bar.jar
.
I have added d:\progs\
to my Path environment variable.
Now i do cd c:\anotherdir
.
In this new dir i do foo -v
. But i get 开发者_C百科the following error :
Unable to access jarfile bar.jar
How can i get the current path of foo.bar
since %CD% returns c:\anotherdir
?
The code snippet you're looking for is %~dp0
. It gives you the path of the currently running batch job.
This also has the advantage of allowing you to run the command from any location, whereas capturing %CD%
in the beginning of your batch script will capture the directory that you were in when you started the batch script. (If this is what you want to do, however, that would be the preferred solution, as suggested by Richard.)
I have added d:\progs\ to my Path environment variable.
That only affects launching programs, it doesn't help with arguments (eg. data files and documents) passed to the program.
How can i get the current path of foo.bar since %CD% returns c:\anotherdir ?
Use %CD%
to capture the current folder, and thus build the data file's path before changing directory.
精彩评论