user2245624
user2245624

Reputation: 149

How do I get the drive letter a batch script is running from?

I have a batch script on a CD. Whenever I try to run it and enter %~d0, it returns the C: drive instead of F:, which is my CD drive.

What is a way to find the drive letter?

Upvotes: 14

Views: 27409

Answers (2)

user1000456
user1000456

Reputation:

You can use %~dp0 to get the current/working directory:

%~d0

Upvotes: 1

bubba
bubba

Reputation: 3862

Get the drive letter from the current directory with:

%cd:~0,2%

%~dp0 is pretty useful in a bat: it is the folder in which the executing bat file resides.

Perhaps at the top of your script, do something like:

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0

and then echo it out to debug. %~d0 should be giving you what you want, but the other options I mentioned might be helpful in solving the challenge.

Upvotes: 26

Related Questions