Results 1 to 9 of 9

Thread: Seeing if a file exist

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313

    Question

    ok, befor I open a file, I want to make sure the path, and the file exist. How do I do that?

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Code:
        if len(Dir$(path)) <> 0 Then "File Exists"

  3. #3
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804
    Try:

    if len(dir$(path))=0 then 'file doesn't exist

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    That was weird, almost the exact same syntax...

  5. #5
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804
    Yup, this has got to be one of the most common questions hey Lethal?

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    yeah, it's in the top ten..

  7. #7
    Addicted Member GungaDin's Avatar
    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    146
    You just have to be a bit careful as the Dir function will give you an error if you try and access an invalid drive or drive without a disk (eg A Drive)

    For example:

    Dir("A:\test.txt") will give an error if there is no disk in the A drive. The following function takes care of this.

    Code:
    Public Function FileExists(FileName As String) As Boolean
    
        On Error GoTo FileNotFound
    
        If Len(Dir$(FileName)) <> 0 Then
            FileExists = True
        Else
            FileExists = False
        End If
    
    Exit Function
    
    FileNotFound:
        FileExists = False
        
    End Function
    Most of the time, this a complete waste of energy but this has pissed me off in the past.

    Hope this helps,

    Nathan Liebke

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Or simply use the On Error Resume Next or another inline error handler routine. I was presumming he was going to do some validation before porting it to "Live Data".

  9. #9
    Addicted Member GungaDin's Avatar
    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    146
    Yep, that would work too.

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