Results 1 to 19 of 19

Thread: Check if file exists

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Check if file exists

    VB Code:
    1. Private Function CheckForFile(FileName) As Boolean
    2. 'Check if file exists
    3. CheckForFile = (Dir(FileName) <> "")
    4. End Function
    5.  
    6. Private Sub Form_Load()
    7. If CheckForFile(App.Path & "\Yourfile") Then
    8. 'Do the correct thing
    9. Else
    10. 'Do the incorrect thing
    11. End If
    12. End Sub

    Thanks Si_The_Geek for updating my previous code
    Last edited by Madboy; Dec 3rd, 2003 at 12:02 PM.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    This has been posted more than once before...


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Just keeping everyone up to date,

    Not everybody backtrack threads you know

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    San Jose, Ca. - USA
    Posts
    302
    This should be faster

    Code:
    Private Function CheckForFile(byval FileName as string) As Boolean
         'Check if file exists
         CheckForFile = Lenb(Dir(FileName))
    End Function

  5. #5

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Ok, i might try that later

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by Madboy
    Just keeping everyone up to date,

    Not everybody backtrack threads you know
    I thought people would use the use the search feature to find something specific instead of reading through all the posts...


    Has someone helped you? Then you can Rate their helpful post.

  7. #7

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Some n00bs dont know how to search, and even if you do search, you have to backtrack through the list

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    So should I start reposting all the things I posted when the codebank first opened? Anyway, never mind...


    Has someone helped you? Then you can Rate their helpful post.

  9. #9
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    I'm used to this one:

    VB Code:
    1. Public Function Exists(iFileName As String) As Boolean
    2.     'Check filename
    3.     Exists = (Len(Dir(iFileName, vbArchive Or vbHidden Or vbNormal Or vbReadOnly Or vbSystem)) > 0)
    4. End Function

  10. #10

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    In a sense that is better, it searches for all atributes of the file

  11. #11
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346
    But doesn't the Dir command automatically searches all different types of file attributes by default? So what's the sense specifiying the type?

    And secondly...how in anyway is Lenb(Dir....etc) faster that just plain Dir(...)?

    For me the shortest way proves to be the fastest and most easily understood...

    VB Code:
    1. If Dir(FilePath)<>"" Then
    2.     DoThis
    3. Else
    4.     DoThisInstead
    5. End If

    Just my suggestion....
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

  12. #12
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by Protocol
    And secondly...how in anyway is Lenb(Dir....etc) faster that just plain Dir(...)?

    For me the shortest way proves to be the fastest and most easily understood...

    VB Code:
    1. If Dir(FilePath)<>"" Then
    2.     DoThis
    3. Else
    4.     DoThisInstead
    5. End If

    Just my suggestion....
    In general, numeric comparisons are much faster than string comparisons. Shortest code does not always equal fastest code.

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


    Take credit, not responsibility

  13. #13
    Hyperactive Member
    Join Date
    Feb 2003
    Location
    Grenada
    Posts
    346
    @ crptcblade....

    I read yah and I understand what your saying...

    Guess I misphrased when I said "fastest"....
    If my post has been helpful, then please rate it accordingly...
    If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.

  14. #14

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    ....as long as it works thats the main thing for me.

  15. #15
    Addicted Member Chrispybee's Avatar
    Join Date
    Sep 2003
    Location
    North Wales, UK
    Posts
    217
    Ok guys & Gals, the problem i've been having is if a file exists how to I skip. Could you put this in??

    Ta

  16. #16
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by Chrispybee
    Ok guys & Gals, the problem i've been having is if a file exists how to I skip. Could you put this in??

    Ta
    -= a peet post =-

  17. #17

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    how do you mean skip?

  18. #18
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427
    Originally posted by Madboy
    Just keeping everyone up to date,

    Not everybody backtrack threads you know
    Have you read the first thread in this forum which says "In order to avoid duplicating something that is already in the Codebank, please do a search before posting." In other words DON'T "[keep] everyone up to date".

  19. #19

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Sorry, this post is ancient, i know better now

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