up vote 1 down vote favorite

How do I get a list of drive letters and their associated labels on a windows system through a bat file?

link|flag

70% accept rate

4 Answers

up vote 6 down vote accepted

This will get most of it:

Net Use

If you have any drives mapped via subst you would also need to get those:

Subst

For completeness, you would do it like this in Powershell (if you are on windows 7 or have installed it):

gwmi win32_LogicalDisk -filter DriveType=4

You can also do it from the command prompt or a batch file using WMI like this:

wmic logicaldisk get caption,providername,drivetype,volumename
link|flag
This doesn't list local disk drives like any internal hard drives or cdroms. Am I missing something? – ObligatoryMoniker Sep 4 '09 at 21:12
Nope, I misunderstood the question. I thought that you only wanted mapped drives. I will edit. – EBGreen Sep 4 '09 at 21:18
The wmic solution should give you everything. – EBGreen Sep 4 '09 at 21:20
You need to remove the quotes from wmic logicaldisk get "caption,providername,drivetype" so that it is wmic logicaldisk get caption,providername,drivetype other wise it throws an error. EBGreen you have single handedly blown my mind for the day, I have never seen wmic even though I have been scripting various server builds for weeks and I am extremely excited to see what else I can do with it. Great job! – ObligatoryMoniker Sep 4 '09 at 21:24
1  
wmic logicaldisk get caption,providername,drivetype,volumename the VolumeName was what I was missing, if you update yours with that I will accept it as the answer. Thanks – ObligatoryMoniker Sep 4 '09 at 21:31
show 5 more comments
up vote 0 down vote

Somewhat kludgy, but works from a batch file:

echo LIST VOLUME > temp.txt && diskpart /s temp.txt && del /q temp.txt
link|flag
up vote 0 down vote

If anyone is lucky enough to be using Vista (Vista Ultimate SP2 b6002, in my case) and the gwmi and wmic snippets given here don't work exactly, here is what I did to make it work.

For gwmi, if you receive no output, try changing the DriveType to 3. If still having problems, remove the -filter option altogether and analyze output.

gwmi win32_LogicalDisk -filter DriveType=3

For wmic, if you receive "Invalid GET Expression", then try putting the get expression in quotes:

wmic logicaldisk get "caption,providername,drivetype,volumename"
link|flag
up vote -2 down vote
@ECHO OFF
IF NOT EXIST A: GOTO B
:A
VOL A:
:B
IF NOT EXIST B: GOTO C
VOL B:
:C
IF NOT EXIST C: GOTO D
VOL C:
:D
IF NOT EXIST D: GOTO E
VOL D:
:E
IF NOT EXIST E: GOTO F
VOL E:
:F
IF NOT EXIST F: GOTO G
VOL F:
:G
IF NOT EXIST G: GOTO H
VOL G:
:H
IF NOT EXIST H: GOTO I
VOL H:
:I
IF NOT EXIST I: GOTO J
VOL I:
:J
IF NOT EXIST J: GOTO K
VOL J:
:K
IF NOT EXIST K: GOTO L
VOL K:
:L
IF NOT EXIST L: GOTO M
VOL L:
:M
IF NOT EXIST M: GOTO N
VOL M:
:N
IF NOT EXIST N: GOTO O
VOL N:
:O
IF NOT EXIST O: GOTO P
VOL O:
:P
IF NOT EXIST P: GOTO Q
VOL P:
:Q
IF NOT EXIST Q: GOTO R
VOL Q:
:R
IF NOT EXIST R: GOTO S
VOL R:
:S
IF NOT EXIST S: GOTO T
VOL S:
:T
IF NOT EXIST T: GOTO U
VOL T:
:U
IF NOT EXIST U: GOTO V
VOL U:
:V
IF NOT EXIST V: GOTO W
VOL V:
:W
IF NOT EXIST w: GOTO X
VOL W:
:X
IF NOT EXIST X: GOTO Y
VOL X:
:Y
IF NOT EXIST Y: GOTO Z
VOL Y:
:Z
IF NOT EXIST Z: GOTO END
VOL Z:
:END
link|flag
Um, wow. So if you can distill that into something that would actually just output the drive letters and labels that exist on the system that would be closer to the answer to the question though this is it least movement in the right direction. – ObligatoryMoniker Sep 4 '09 at 21:16
I'm getting down-voted for providing a solution that meets the criteria? Wow... – Scott Sep 5 '09 at 5:19
2  
Well, for one thing, it produces errors for drives that exist but contain no media, such as a CD/DVD drive. – Dennis Williamson Sep 6 '09 at 12:47

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.