Visual Basic 6: Check directory or file exists

12 September 2009

When opening files or directories from within a Visual Basic application you need to check that the file or directory exist, rather than crashing the appliaction and returning un-necessary errors.

The following 2 functions I use to see if the file and directories exist.

The functions will return the value "true" if the file or drectory do exist, and "false" if not.

Public Function FileExists(filename As String) As Boolean
    On Error GoTo ErrorHandler
    FileExists = (GetAttr(filename) And vbDirectory) = 0
ErrorHandler:
End Function

Function DirExists(DirName As String) As Boolean
    On Error GoTo ErrorHandler
    DirExists = GetAttr(DirName) And vbDirectory
ErrorHandler:
End Function

http://www.peternichol.com/entry/trackback/151/

Please leave a comment using the form provided.