Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding Available drive letters

Posted on 2004-03-30
6
Medium Priority
?
318 Views
Last Modified: 2010-05-02

I need to obtain a listing of drive letters that are currently not being used by the local system running the application.  These drive letters will be used to populate a combo box, which the user can select.  How is this action performed?

0
Comment
Question by:escheider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
g0rath earned 200 total points
ID: 10716981
Option Explicit
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Private Sub PopulateComboBox()
      Dim driveType As Integer
      Dim NextDrive As String
      Dim i As Integer
     
      For i = 0 To 25
        NextDrive = Chr(i + 65) + ":\"
        driveType = GetDriveType(NextDrive)
        If driveType = 1 Then
            Combo1.AddItem NextDrive
        End If
        Debug.Print NextDrive
      Next i
End Sub
   
Private Sub Form_Load()
    PopulateComboBox
End Sub
      
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10716997
btw from the API docs, here are the constants

DRIVE_CDROM = 5
A CD-ROM drive.
DRIVE_FIXED = 3
A hard drive.
DRIVE_RAMDISK = 6
A RAM disk.
DRIVE_REMOTE = 4
A network drive or a drive located on a network server.
DRIVE_REMOVABLE = 2
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10717020
yeah and of course

DRIVE_UNKNOWN = 1
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Ryan Chong
ID: 10717039
Or try this:

in a module:

Public Declare Function GetLogicalDrives Lib "kernel32" () As Long
Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Public Sub GetDrives(ByRef myControl As Object, ByVal Select1stItem As Boolean, Optional ClearItems As Boolean = True, Optional ReturnCompleteDriveName As Boolean = True, Optional ReturnCDROMDriveOnly As Boolean = False)
    On Error GoTo ErrHandler
    Dim LDs As Long, cnt As Long, strSave As String
       
    If ClearItems = True Then myControl.Clear
   
    If ReturnCompleteDriveName = True Then
        'Create a buffer to store all the drives
        strSave = String(255, Chr$(0))
        'Get all the drives
        LDs = GetLogicalDriveStrings(255, strSave)
        'Extract the drives from the buffer and print them on the form
        For cnt = 1 To 25
            If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
            If ReturnCDROMDriveOnly = True Then
                If GetVolumnInfo(Left$(strSave, InStr(1, strSave, Chr$(0)) - 1), viFileSystem) = "CDFS" Then
                    myControl.AddItem Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
                End If
            Else
                myControl.AddItem Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
            End If
            strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
        Next cnt
    Else
        'get the available drives
        LDs = GetLogicalDrives
        For cnt = 0 To 25
            If (LDs And 2 ^ cnt) <> 0 Then
                If ReturnCDROMDriveOnly = True Then
                    If GetVolumnInfo(Chr$(65 + cnt), viFileSystem) = "CDFS" Then
                        myControl.AddItem Chr$(65 + cnt)
                    End If
                Else
                    myControl.AddItem Chr$(65 + cnt)
                End If
            End If
        Next cnt
    End If
   
    If Select1stItem = True And myControl.ListCount > 0 Then myControl.ListIndex = 0
    Exit Sub
ErrHandler:
    ShowErrMsg
End Sub

then use it like:

GetDrives Combo1, True, True

Hope this helps too
0
 
LVL 5

Expert Comment

by:g0rath
ID: 10717088
well really you should look up 0 or 1 since it's different in the old days....so if you're concerned with OS compatiblilty

DRIVE_UNKNOWN = 0
DRIVE_NO_ROOT_DIR = 1
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 10717126
and paste this in the module as well:

Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

Public Enum VolumnInfo
    viVolumn = 1
    viFileSystem = 2
    viSerial = 3
End Enum
Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

Public Function GetVolumnInfo(ByVal DriveName As String, ByVal ReturnInfo As VolumnInfo)
    Dim Serial As Long, VName As String, FSName As String
   
    If Len(DriveName) > 3 Then DriveName = Left$(DriveName, 3)
   
    If InStr(1, DriveName, ":") = 0 Then DriveName = DriveName & ":"
   
    'Create buffers
    VName = String$(255, Chr$(0))
    FSName = String$(255, Chr$(0))
    'Get the volume information
    GetVolumeInformation DriveName, VName, 255, Serial, 0, 0, FSName, 255
    'Strip the extra chr$(0)'s
    VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
    FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
    Serial = Trim(Str$(Serial))
    Select Case ReturnInfo
    Case 1
        GetVolumnInfo = VName
    Case 2
        GetVolumnInfo = FSName
    Case 3
        GetVolumnInfo = Serial
    End Select
End Function
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question