I have a batch file which is on a usb key. I need to know the drive name the batch is in.
Example, if it\'s E:\\mybatch.bat it should find E:\\ same thing for F:\\, G:\\
The existing answers to this question don't acknowledge that the question is actually asking about two different things:
These can be different when the batch file is started from a working directory other than its residing location. Readers should therefore be clear on the difference before determining which solution is relevant to their case.
%CD:~0,2%
This takes the full current working directory and trims it down to the first two characters, which will be a drive letter and a colon, e.g. C:
.
%~d0
This trims the full path of the batch file (in %0
) to just a drive letter and a colon, e.g. C:
.
(this is an expanded version of my rejected edit to Kai K's answer)