Results 1 to 11 of 11

Thread: MY FileExists function

Threaded View

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    MY FileExists function

    Since in the last few days there have been (for some reason) a lot of questions about whether a file exists, try this:

    VB Code:
    1. Public Function FileExists(Filename As String, _
    2.         Optional TreatDirAsFile As Boolean = False) As Boolean
    3. ' Returns true if Filename exists. If Filename is a directory and
    4. ' TreatDirAsFile is true then the function will also return true.
    5. ' Similarly if the file is really a directory and TreatDirAsFile is false
    6. ' then the function will return false.
    7.  
    8.     On Error Goto FileIOError ' catch a 404
    9.  
    10.     Dim Attributes As Integer
    11.     Attributes = GetAttr(Filename) ' will throw an error if it doesn't exist
    12.     If Attributes = vbDirectory And Not TreatDirAsFile Then
    13.         FileExists = False ' it's a directory but that's not cool
    14.     Else
    15.         FileExists = True ' it's a directory but we said that's cool
    16.     End If
    17.    
    18.     Exit Function
    19.    
    20.     FileIOError:
    21.         FileExists = False ' error thrown at GetAttr, file doesn't exist
    22. End Function
    The TreatDirAsFile will make the function return true if Filename is a directory. But I haven't tested it, does it work?
    Last edited by filburt1; Dec 7th, 2001 at 05:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width