Results 1 to 5 of 5

Thread: [RESOLVED] How to check whether a file exists

  1. #1

    Thread Starter
    New Member kitty500cat's Avatar
    Join Date
    Aug 2006
    Posts
    2

    Resolved [RESOLVED] How to check whether a file exists

    What code would you use to get the program to report if a text file exists? Like if you want to write or overwrite a text file, but you want the program to say if the file doesn't yet exist, how would you do that?
    Thanks
    kitty500cat
    Last edited by Hack; Aug 4th, 2006 at 06:28 AM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark
    I like cats; they taste like chicken.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to check whether a file exists

    VB Code:
    1. Public Function FileExists(ByVal sFileName As String) As Boolean
    2.     FileExists = (Len(Dir$(sFileName, vbHidden Or vbSystem Or vbReadOnly)) > 0)
    3. End Function
    You can call the above function like this:
    VB Code:
    1. If FileExists("c:\theFile.txt") = False Then
    2.     MsgBox "The file does not exist!", vbInformation
    3. End If

  3. #3
    Junior Member
    Join Date
    Aug 2006
    Location
    Montreal, Quebec
    Posts
    27

    Re: How to check whether a file exists

    Code:
    if dir("c:\filename.txt") <> "" then
         'file exist
    else
         'File Does Not Exist
    end if

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: How to check whether a file exists

    Or You can Use the file system object

    VB Code:
    1. Dim fso as new file system object
    2. if fso.fileexist("filname with Path")=true then
    3. msgbox "file exist"
    4. else
    5. msgbox "file not exist"
    end if

  5. #5

    Thread Starter
    New Member kitty500cat's Avatar
    Join Date
    Aug 2006
    Posts
    2

    Re: How to check whether a file exists

    Thanks guys, I have known some basic stuff about VB6 for maybe 2 years, but I don't know terribly much yet; I just started getting into it again. I didn't know how to use the Dir function or whatever it's called, but this stuff answers my questions.
    thanks again
    kitty500cat
    I like cats; they taste like chicken.

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