Results 1 to 11 of 11

Thread: MY FileExists function

  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.

  2. #2
    DaoK
    Guest
    Can I know why you do not have vb?

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I'm at work and I refuse to open the VBA editor.

  4. #4
    DaoK
    Guest
    It work LIKE A CHARM!!!!!!!!!!!!!!!!!!! it's so damn sexy!!! but that code in your tips page

  5. #5

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I think I will...later. I'm going to wait for TurtleTips.com to go live first before I do more web stuff probably.

  6. #6

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Anybody else care to comment or see a bug?

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Not bugs, but I would add some code the make sure what was in Filename. In other words, check to see if it's something like "C:\path\myfile" or just "myfile". I might also consider returning a long with the following values:

    -1 means file does not exist
    0 means file exists
    any other value is the err.number in case an error occurs.

  8. #8
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Uhh i was just wondering why you can't use the Dir() function? It seems to support UHC paths and everything...?
    You just proved that sig advertisements work.

  9. #9

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    It will say that the file "*" exists and other stuff.

  10. #10
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by filburt1
    It will say that the file "*" exists and other stuff.
    What?
    You just proved that sig advertisements work.

  11. #11

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    The Dir() function works for filters, so if you asked if the file "*.txt" exists then it will return true, even though "*.txt" isn't a valid filename because of the wildcard.

    And let's not even mention the FSO.

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