Skip to main content

Check if File or Directory Exists

This snippet will check if a file or directory exists.  It returns true or false and you send the full pathname and filename.  I adapted this snippet from one I found at http://www.vbaexpress.com/kb/getarticle.php?kb_id=559

Snippet

Function FileOrDirExists(PathName As String) As Boolean
        'Macro Purpose: Function returns TRUE if the specified file
    '               or folder exists, false if not.
    'PathName     : Supports Windows mapped drives or UNC
    '             : Supports Macintosh paths
    'File usage   : Provide full file path and extension
    'Folder usage : Provide full folder path
    '               Accepts with/without trailing "\" (Windows)
    '               Accepts with/without trailing ":" (Macintosh)
    
    Dim iTemp As Integer
    
    'Ignore errors to allow for error evaluation
    On Error Resume Next
    iTemp = GetAttr(PathName)
    
    'Check if error exists and set response appropriately
    Select Case Err.Number
        Case Is = 0
            FileOrDirExists = True
        Case Else
            FileOrDirExists = False
    End Select
    Err.Number = 0
    
    'Resume error checking
    On Error GoTo 0
End Function