简体   繁体   中英

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\ .
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 .)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM