IsDriveExist




Option Explicit
Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
'Add the following code to Form1.

Private Sub Form_Load()
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

End Sub

Private Sub cmdEnd_Click()
Unload Me
End
End Sub

Private Sub cmdCheckDrive_Click()
Dim drv2Find As String
drv2Find$ = "D"

If DriveExists(drv2Find$) Then
Label1 = "Drive " & drv2Find$ & ": exists on this system."
Else: Label1 = "Drive " & drv2Find$ & ": was not found."
End If
End Sub

Private Function DriveExists(drvName$)

'retrieve the available drives, and check each against the passed drive name

'working variables

Dim r As Long
Dim allDrives As String

'pad the string with spaces

allDrives$ = Space$(64)

'call the API to get the string containing all drives

r& = GetLogicalDriveStrings(Len(allDrives$), allDrives$)

'do a instr search, using text compare (not case sensitive)

If InStr(1, allDrives$, drvName$, 1) Then DriveExists = True

End Function

This code is intended to demonstrate how to determine if a
drive exists on the target system.
Start a new project, and to the form add 2 command buttons
(cmdCheckDrive and cmdEnd) and an autosizing label (Label1) as
indicated in the illustration. Place the following API declare
code into the general declarations area of a bas module:










( isdriveexist.html )- by Paolo Puglisi - Modifica del 17/12/2023