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

batch-file, drive, scripting

Solution

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.

Problem

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?

Original source