?
Solved

Finding Available drive letters

Posted on 2004-03-30
6
Medium Priority
?
314 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 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