Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check For Existance of Drive.

Status
Not open for further replies.

Ed2020

Programmer
Nov 12, 2001
1,899
GB
I want to check to see if a user of one of my system has an E:\ drive set up on their machine.

I thought I could do this with the Dir() function, but this doesn't seem to work.

Anybody got any ideas?

Ed Metcalfe.
 
Here's a solution. The function will change the drive. If the drive does not exist, the function returns FALSE, otherwise TRUE.

Code:
Sub Main()
    MsgBox (CheckDrive("D"))
End Sub
Function CheckDrive(sDrive As String) As Boolean
    On Error Resume Next
    
    'change drive
    ChDrive sDrive
    
    If Err.Number <> 0 Then
        CheckDrive = False
    Else
        CheckDrive = True
    End If
    
End Function
 
...or if you dont want to change drive:

Declare Function WNetGetConnection Lib &quot;mpr.dll&quot; Alias &quot;WNetGetConnectionA&quot; _
(ByVal lpszLocalName As String, _
ByVal lpszRemoteName As String, _
cbRemoteName As Long) As Long

Function NetworkDriveExists(i_strDriveLetter As String, ByRef o_strUNCPath As String) As Boolean

Dim lngReturn As Long
Dim strLocalName As String
Dim strRemoteName As String
Dim lngRemoteName As Long

If Right(i_strDriveLetter, 1) <> &quot;:&quot; Then
i_strDriveLetter = i_strDriveLetter & &quot;:&quot;
End If
strRemoteName = String$(255, Chr$(32))
lngRemoteName = Len(strRemoteName)

If WNetGetConnection(i_strDriveLetter, strRemoteName, lngRemoteName) = 0 Then

NetworkDriveExists = True
o_strUNCPath = Left(strRemoteName, InStr(strRemoteName, Chr(0)) - 1)
Else
NetworkDriveExists = False
o_strUNCPath = vbNullString
End If

End Function

This will also return the UNC for the drive letter specified (if you fancy that sort of thing)

M :)
 
Thanks all. Problem solved.

Ed Metcalfe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top