#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Midlands, UK
    Posts
    40
    Rep Power
    12

    Question An easy way to check to see if a file exists in VB6?


    Hi, Is there an easy way in VB6 to check to see if a file exists?

    It seems daft to have to try and open it and then trap and error.

    All I want to do is check to see it a file exists and if it does then I want to copy it to another folder.

    I can't get it to work.

    All the ON ERROR GOTO label stuff really makes a mess of the structure of my program.

    Is there a way like in C :- handle = fopen(.... etc etc) and then handle is NULL if the file doesn't exist? That seems a more elegant way of doing it.

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    34
    Rep Power
    12

    Lightbulb File Exist In VB?


    May be this helps....
    Code:
    'Assume That The File Is At "C:\What\File.txt"
    If Dir("C\What\File.txt") <>"" Then
    FileCopy "C:\What\File.txt", "C:\Where\File.txt"
    Else
    MsgBox "File Does Not Exist!"
    End If
    I May Have Misinterpret U'r Post
    Correct Me If I Am Wrong......//
    Enjoy Coding..........................///

    zak2zak
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Midlands, UK
    Posts
    40
    Rep Power
    12

    Re: File Exist In VB?


    Originally posted by zak2zak
    May be this helps....
    Yes. Thank you VERY much. That has done the trick
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2003
    Posts
    1,152
    Rep Power
    13

    Talking


    In anther way:
    if Len(dir(C\What\File.txt") >0 Then
    FileCopy "C:\What\File.txt", "C:\Where\File.txt"
    Else
    MsgBox "File Does Not Exist!"
    End If

    or Use Ms srcipt object...
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    2
    Rep Power
    0
    What is the syntax if I want to check for file in same directory as executable?

    For example, I have an executable that relys on an INI file for info. The INI file should be located in the same location as the EXE file. So I need to check to see if INI file is there.

    This doesn't seem to work:

    If Dir("File.ini") <>"" Then
    MsgBox "File Exists!"
    Else
    MsgBox "File Does Not Exist!"
    End If
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    34
    Rep Power
    12

    Lightbulb Check INI In Same Dir


    May Be This Helps..
    Code:
    If Dir(App.Path & "\File.ini") <>"" Then
    MsgBox "File Exists!" 
    'Statement here
    Else
    MsgBox "File Does Not Exist!"
    End If
    I May Have Misinterpret U'r Post
    Correct Me If I Am Wrong......//
    Enjoy Coding..........................///

    zak2zak
  12. #7
  13. A mule with a spinning wheel.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Wales
    Posts
    117
    Rep Power
    13

    FileSystemObject Solution


    If you are using the FileSystemObject then

    FileSystemObject.FileExists(filePath)

    Returns True or False. filePath can by absolute or relative.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    2
    Rep Power
    0

    Re: Check INI In Same Dir


    Originally posted by zak2zak
    May Be This Helps..
    Code:
    If Dir(App.Path & "\File.ini") <>"" Then
    MsgBox "File Exists!" 
    'Statement here
    Else
    MsgBox "File Does Not Exist!"
    End If

    This did the trick. Thanks a bunch.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Thumbs up


    Hello Guys..
    It's really help me..
    Very" helping me on finishing my exam..
    Thanx guys..
  18. #10
  19. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,443
    Rep Power
    4539
    Originally Posted by hary_ps
    Hello Guys..
    It's really help me..
    Very" helping me on finishing my exam..
    Thanx guys..
    I'm happy to hear this topic was helpful, but next time please check the date, this thread is 6 years old ;-)
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0
    ^
    Haha..
    I just noticed about the date..

    I was find the thread by googling..
    I'm just register this forum member too(just wanna say thanks maybe).

    I'm in indonesia n maybe my english is bad. Hehe.. :P

    Overall very thanks to you guys..
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0
    One thing to keep in mind, and I am still researching it, is that 'Dir' seems to ignore System files. I wasn't aware of that until yesterday.

    That check for file length is a life saver.

IMN logo majestic logo threadwatch logo seochat tools logo