Results 1 to 6 of 6

Thread: Open function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Post

    How can I check if I a file exists when using the open function?

    e.g

    Open "c:\filename.txt" For Input As #1

    if the file does`nt exist a "run-time" error window occurs. Is there anyway to stop this?
    Either a

    exists = doesfileexist("C\filename.txt")

    or an extension of the Open function?

    Thanks in advance.

  2. #2
    Lively Member Maartin's Avatar
    Join Date
    Jan 2000
    Location
    Benoni, Gauteng, South-Africa
    Posts
    99

    Post

    The only true VB way to check if a file exists is to use the dir function.

    The Open function will create the file for you if it does not exist.

    So to check, use the following:

    If Dir("c:\filename.txt") <> "" Then
    '|File does exist
    Else
    '|File does not exist
    End If

    I don't know where your runtime error comes from; it might be syntax error or something like that.
    If you want to, send me the code and I’ll have a look at it for you. Or else I will create you a small sample.

    Just let me know.

    Hope it helps.

    -----------------------
    Maartin
    [email protected]
    -----------------------


    [This message has been edited by Maartin (edited 01-19-2000).]

  3. #3
    Hyperactive Member venkatraman_r's Avatar
    Join Date
    Jul 1999
    Location
    Chennai, INDIA
    Posts
    284

    Post

    Hi,

    You can use this:

    if Len(Dir(Filename)>0then
    msgbox "File does not exist"
    else
    ''do something
    end if.

    First you have to get the filename in a variable and try this.

    Hope it helps. Good luck.

    ------------------
    Regards,

    Venkat

    [email protected]
    ICQ: 45714766
    http://venkat.iscool.net


  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Post

    Cheers chaps, that works a treat

    [This message has been edited by Rick H (edited 01-19-2000).]

  5. #5
    Junior Member
    Join Date
    Jan 2000
    Posts
    21

    Post

    Just a quick comment. First of all Rick H got the error because he was trying to open a file that didn't exist. Second, you can get errors if you try to use Dir() together with the filename AND the full path, if the path doesn't exist. You can check if a path exist by using Dir(path$, vbDirectory).

  6. #6
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386

    Post

    Yes, but you can also just easily trap the error...
    Code:
        On error goto errhandler  
        Dim iFile As Integer
        iFile = FreeFile
        Open PathAndFileName For Input As #iFile
            ' do something
        Close #iFile
    
        Exit Sub ' or function
    errhandler:
        ' trap the several errors that can occur here...
        ' don't forget to close the file if another error occured then "does not exist"
        msgbox Err.Number & "  -  " & Err.Description

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