main page >programming tips (VB5/6) >  Check if a folder exists     diese Seite auf deutsch diese Seite auf deutsch
Here is a function that checks whether a particular folder exists, without any dependency to a library like the FSO. Whenever the folder does not exist, or access to a drive that isn't ready is attempted, the error is trapped and the return value is set accordingly. Oftenly people use "If Dir$(Filename, vbDirectory) <> "" Then ..." for this check, but this method is not recommended since Dir$ is not re-entrant; that means that a call to Dir$ within an outer Dir$-Loop would terminate the outer loop.

Public Function FolderExists(ByVal Path as String) As Boolean
Dim i As Integer
On Error Resume Next
i = GetAttr(Path)
  If Err.Number = 0 Then
    FolderExists = CBool(i And vbDirectory)
  End If
On Error GoTo 0
End Function

Update: As mentioned in the article "Check if a file exists", GetAttr() is a little bit outdated. Since GetAttr() is only a wrapper for the API function GetFileAttributes(), it's better to call GetFileAttributes() directly:
Public Declare Function GetFileAttributes Lib "kernel32" _
  Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long

Public Function FolderExists(ByVal Filename As String) As Boolean
Dim l As Long
l = GetFileAttributes(Filename)
    FolderExists = CBool(l And vbDirectory)
  End If
End Function
main page >  programming tips (VB5/6) >  this page