14

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2245624
  • 149
  • 1
  • 1
  • 4

2 Answers2

26

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.

Gras Double
  • 15,901
  • 8
  • 56
  • 54
bubba
  • 3,839
  • 21
  • 25
  • 1
    Doesn't %cd% give the path you are _executing_ from, though? It is completely inaccurate for getting the actual batch file's path. For example, Windows XP seems to have the bizarre bug that if you drag and drop a file onto a program/script to give that dropped file as argument to the program to execute, the program will mysteriously launch from the c:\Documents and Settings\\(username) directory instead of from its own folder. – Nyerguds Jun 10 '15 at 07:04
  • 1
    Seems it changed since XP, but in Win7 it seems to execute from the path of the parameter file instead... which is still wrong. – Nyerguds Jun 10 '15 at 07:07
1

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

%~d0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131