Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In windows batch scripts, how can I iterate over all drive letters which correspond to physical volumes or mapped shares?

share|improve this question

6 Answers 6

Also going to suggest a language switch, but to Powershell instead of VBS.

Get-PSDrive -PSProvider FileSystem

It's the wave of the future...

share|improve this answer
    
This also yields other drives that don't have anything to do with non-PowerShell things. On my system, Home: is a PSDrive to my home directory, for example. You're getting more back than needed here. –  Joey Apr 18 '10 at 13:04
1  
gwmi win32_logicaldisk | where {$_.DriveType -eq 3} for physical drives, if you want only those with drive letters (possible to not have any mount point, or could be mounted to a folder) add | where {$_.DeviceID -like "*:"}. for network drives just change the original where statement to where {$_DriveType -eq 4} (3 = Local disk, 4 = Network disk). PowerShell is absolutely awesome once you get the hang of these crazy commands. If you list exactly what you're trying to do, I can elaborate more. –  Chris S Apr 18 '10 at 13:24
    
Another powershell option that doesn't pull back powershell-specific drives: [System.IO.DriveInfo]::GetDrives() –  MattB Apr 19 '10 at 13:05

Maybe there are better tools now, but there used to be the fsutil command (WinXP).

fsutil fsinfo drives

this returns all the drives in the system.

share|improve this answer
2  
Vista: "The FSUTIL utility requires that you have administrative privileges." –  Dennis Williamson Apr 15 '10 at 13:57

It's going to be much easier, and far more useful to go with VBScript in that case. http://authors.aspalliance.com/brettb/VBScriptDrivesCollection.asp

share|improve this answer
    
VBScript is a dead language, I wouldn't recommend anybody learn it if they don't know it already. PowerShell does everything VBScript does, plus more, it's widely supported, and more natural to use (CLI). –  Chris S Apr 18 '10 at 13:27

You can coax

wmic volume get driveletter

to be your data source here. Iterating can then be done with standard for /f.

share|improve this answer

You can try:

for %%i in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO @if exist %%i: @echo %%i:

The main limitation with this code is the existance of a CD/DVD drive with no disk. It causes a carp for the user to insert a disk. If you have all CD/DVD drives mapped to Z: you could avoid the carp by removing the final Z in the set.

Rob

share|improve this answer

This works for me from msys, you didn't specify which bash interpreter you are using (the main ones would be msys or cygwin).

for i in `mount|grep "^.: "|cut -c1`; do
  echo iterating over drive $i
done
share|improve this answer
    
The OP didn't say "bash". –  Dennis Williamson Apr 15 '10 at 19:53
    
Oh my bad, I misread 'batch' as 'bash'. Well, I'll leave this post here in case someone wants to iterate in bash in windows in the future. –  davr Apr 15 '10 at 22:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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