VB Quicktakes - Find a Drive

This code uses the file system object to search for drives of a specific type be it cd-rom, zip, floppy, local or network . The new(relatively) file system object allows for easier file manipulation. It can be used to get information about file structures and also to manipulate the file system. Though a useful new tool, I have found one downside to the FSO, it is incredible slow when opening text files for this reason I would not recommed using the fso to open and text files. I feel that the code listed below may be useful and also is a good introduction to the FSO. To run the code listed below, a form with one command button named cmdGetDrive and one list box named lstDrive which contains a list of the drives which may be searched for is necessary.


Option Explicit

Const FLOPPY = 1457664
Const ZIP = 100431872


Private Sub cmdGetDrive_Click() Dim DriveFound As Boolean DriveFound = GetDriveType(lstDrive.ItemData(lstDrive.ListIndex)) If Not DriveFound Then MsgBox "A " & lstDrive.ItemData(lstDrive.ListIndex) & " was not located." & _ " To ensure that a proper search is run, make sure that all " & _ " removeable drives (ex. zip, floppy) have the proper media inserted" _ , vbInformation + vbOKOnly End If End Sub
Private Function GetDriveType(SearchType As String) As Boolean Dim fso As New FileSystemObject Dim drv As Drive Dim totSize As Long, dType As Integer Select Case SearchType Case "CD-ROM" dType = 4 Case "3.5 Floppy" dType = 1 totSize = FLOPPY Case "Zip" dType = 1 totSize = ZIP Case "Network" dType = 3 Case "Local Hard Drive" dType = 2 End Select If SearchType = "CD-ROM" Or SearchType = "Network" Or SearchType = "Local Hard Drive" Then For Each drv In fso.Drives If drv.DriveType = dType Then MsgBox drv.DriveLetter & ":\ is a " & SearchType GetDriveType = True End If Next Else For Each drv In fso.Drives If drv.IsReady Then If drv.DriveType = dType Then If drv.TotalSize > totSize - 50 And drv.TotalSize < totSize + 50 Then MsgBox drv.DriveLetter & ":\ is a " & SearchType GetDriveType = True End If End If End If Next End If End Function
Private Sub Form_Load() lstDrive.SetFocus lstDrive.ListIndex = 0 End Sub
About this page: