Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSee if a file exists four ways
Keywordsfile, exists
CategoriesFiles and Directories
 
Use Dir$, Open, FileLen and GetAttr. Note that Open may cause trouble if the file is locked by another application.

Thanks to Tom Steemson

 
Private Function FileExistsWithDir(ByVal filename As String)
Dim file_name As String

    On Error Resume Next
    file_name = Dir$(filename)
    FileExistsWithDir = (file_name <> "")
End Function

Private Function FileExistsWithOpen(ByVal filename As _
    String)
Dim fnum As String

    fnum = FreeFile
    On Error GoTo FileDoesntExist
    Open filename For Input As fnum
    Close fnum
    FileExistsWithOpen = True
    Exit Function
    
FileDoesntExist:
    FileExistsWithOpen = False
End Function

Private Function FileExistsWithFileLen(ByVal filename As _
    String)
Dim length As Long

    On Error GoTo FileDoesntExist
    length = FileLen(filename)
    FileExistsWithFileLen = True
    Exit Function
    
FileDoesntExist:
    FileExistsWithFileLen = False
End Function

Private Function FileExistsWithAttrib(txt)
Dim lRetVal As Long
    
    On Error GoTo FileDoesntExist:
    lRetVal = GetAttr(txt)
    FileExistsWithAttrib = True
    Exit Function
    
FileDoesntExist:
    FileExistsWithAttrib = False
End Function
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated