Results 1 to 9 of 9

Thread: Test for File

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    How would I test to see if a file exists before I try to open it??

  2. #2
    Junior Member
    Join Date
    Mar 2000
    Location
    Davis, CA
    Posts
    23
    Code:
    If Dir("C:\Windows\file.txt", vbHidden) = "" Then
        Open "C:\Windows\file.txt" For Output As #1
             print #1, variable
        Close #1
    Else
    Open "C:\Windows\file.txt" For Input As #1
             Line Input #1, variable
        Close #1
    End If
    [Edited by sp007 on 08-29-2000 at 10:48 PM]

  3. #3
    Lively Member
    Join Date
    Aug 2000
    Posts
    114

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    An easier why is to use the filesystemobject
    you'll have to reference the Microsoft Scripting Runtime thing first

    Private Sub cmdCheckForFile_Click()
    Dim fs
    Set fs = New Scripting.FileSystemObject
    Exists = fs.FileExists(fs.BuildPath(App.Path, "YourFileHere.txt"))
    'Exists is now a boolean variable with the results T/F of
    'if the file exists or not
    MsgBox Exists

    End sub

  5. #5

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    not working

    This method is returning "false" whether the file is there or not...any thoughts??

  6. #6
    Guest
    Try this:

    Code:
    If Dir("C:\file.txt") <> "" Then
    Open "C:\file.txt" For Input As #1 
    Text1.Text = Input$(LOF(1), 1) 
    Close #1 
    Else
    Msgbox "File does not exist!", vbCritical
    End If

  7. #7
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Re: not working

    Originally posted by crptcblade
    This method is returning "false" whether the file is there or not...any thoughts??
    Be careful with the code above that you used. you'll notice that it had app.path etc so the position of the file was preset (which is likely why you never found a file you knew was there).

    try it like this

    Code:
    Function ReportFileStatus(filespec)
      Dim fso, msg
      Set fso = CreateObject("Scripting.FileSystemObject")
      If (fso.FileExists(filespec)) Then
        msg = filespec & " exists."
      Else
        msg = filespec & " doesn't exist."
      End If
      ReportFileStatus = msg
    End Function
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  8. #8
    Member
    Join Date
    Jul 2000
    Location
    BFE in Oregon
    Posts
    33

    Reusable Function from Books Online -- VB5 SP4

    Function FileExists (p As String) As Long
    If Dir (p) <> " " Then
    FileExists = conSuccess ' Return a constant
    ' indicating the
    Else ' file exists.
    FileExists = conFailure ' Return failure
    ' constant.
    End If
    End Function

    Dim ResultValue As Long
    ResultValue = FileExists ("C:\Testfile.txt")
    If ResultValue = conFailure Then
    .
    . ' Handle the error.
    .
    Else
    .
    . ' Proceed with the program.
    .
    End If

  9. #9
    Member
    Join Date
    Jul 2000
    Location
    BFE in Oregon
    Posts
    33

    Even Smaller

    Function FileExists (filename) As Boolean
    FileExists = (Dir(filename) <> "")
    End Function

    'Returns true if file exists, false if it doesn't

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