Results 1 to 5 of 5

Thread: Checking if a file exists

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 1999
    Posts
    77

    Checking if a file exists

    Is there an efficient way of checking if a file exists? =)

    If so please explain. I've always been poor in this area, and always done it the cheap and unefficient way. Hehehe

    Thanks

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. If Len(Dir$("C:\myFile.txt")) > 0 Then
    2.   MsgBox "File Exists"
    3. Else
    4.   MsgBox "File Not Found"
    5. End If

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    you could drop the Len() couldn't you? Dir() works just fine.

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    You could also check for error numbers when opening a file using Err.Number ... this allows u to check for all file problems such as not existing, already open, no directory etc etc etc etc... look up Err.Number in help for all of the possible checks.

    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    crptcblade is spot on, checking the length of returned value from dir is much faster than doing a string comparison and is therefore the optimised approach.

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