Returns the drive letter using an index : Drive « File Path « VBA / Excel / Access / Word






Returns the drive letter using an index

 

Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Function DriveName(index As Integer) As String
    Dim Buffer As String * 255
    Dim BuffLen As Long
    Dim TheDrive As String
    Dim DriveCount As Integer
   
    BuffLen = GetLogicalDriveStrings(Len(Buffer), Buffer)
    TheDrive = ""
    DriveCount = 0
    For i = 1 To BuffLen
        If Asc(Mid(Buffer, i, 1)) <> 0 Then _
          TheDrive = TheDrive & Mid(Buffer, i, 1)
        If Asc(Mid(Buffer, i, 1)) = 0 Then 'null separates drives
            DriveCount = DriveCount + 1
            If DriveCount = index Then
                DriveName = UCase(Left(TheDrive, 1))
                Exit Function
            End If
            TheDrive = ""
        End If
    Next i
End Function

Sub Main()
  Debug.Print DriveName(3)
End Sub

 








Related examples in the same category

1.Get Drive Information
2.The TypeOfDrive Function
3.The NumberOfBytesFree Function
4.Returns the number of free bytes for a drive
5.Returns the total storage capacity for a drive
6.Returns a string that describes the drive type
7.Returns True if a specified drive letter exists
8.Returns the number of drives
9.Display Drive information