I have a batch foo.bar in a directory d:\progs\.
In this directory I have a jar file called 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 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.)