Cry How To...
List drive letters in a batch file or in a command window
At the command prompt
There are at least two ways of getting a list of drive letters when working at the command prompt.
1. The simplest is to use WMIC, for example:
wmic logicaldisk get description,name
which on my PC produces:
C:\>wmic logicaldisk get description,name
Description Name
Local Fixed Disk C:
CD-ROM Disc D:
Local Fixed Disk E:
Local Fixed Disk F:
Removable Disk G:
Removable Disk H:
Network Connection I:
Removable Disk Z:
If I only wanted to list my local fixed disks I would instead use:
wmic logicaldisk where drivetype=3 get description,name
and this would produce:
C:\>wmic logicaldisk where drivetype=3 get description,name
Description Name
Local Fixed Disk C:
Local Fixed Disk E:
Local Fixed Disk F:
If you specify the "drivetype" the different values you can use are:
Value | Meaning |
---|---|
0 | Unknown |
1 | No root directory |
2 | Removable disk |
3 | Local disk |
4 | Network drive |
5 | Compact disk |
6 | RAM disk |
2. An alternative is to use fsutil
:
fsutil fsinfo drives
which on my PC produces:
C:\>fsutil fsinfo drives Drives: C:\ D:\ E:\ F:\ G:\ H:\ Z:\
As you can see the output is different, fsutil
doesn't return any of
my mapped network drives. The other thing to note about fsutil
is that
it requires administrative privileges so you must "run as administrator"
to use it.
For more information on fsutil
see this
Microsoft Technet article.
In a batch file
For a batch file I prefer to use wmic simply because it does not require elevated privileges. The following when copied into a batch/command file will simply echo the drive letters:
@echo off
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=3"
get name /format:value') do echo %%d
Note that the "for" line should all be on one line - despite how it might be wrapped in the browser window.
When using this for real you will probably want to replace the echo with a call to do something on the drive.
These notes have been tested with Windows Vista and may also apply to other versions of Windows.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.