Results 1 to 5 of 5

Thread: Check "FileExists" before continue with procedure.

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Australia
    Posts
    10

    Smile

    My program needs to extract Data from a text file, how can I check that the file "C:\Myfile.txt" exists using a control button click before running the rest of the Procedure within the control.

    If the File exists ( I want to continue with the procedure )
    If not ( I wish to end the Procedure and show a different Form - telling the user the file does not exist etc. )

    Can you please help.
    Tony Dow

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    Private Sub Command1_Click()
    
      If Len(Dir$("C:\myfile.txt"))=0 Then
        MsgBox "File Doesn't Exist!"
        Exit Sub
      End If
    
      'Otherwise
     
       Open "C:\myfile.txt" For Input As #1
       'do your work...
       Close #1
    End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    Example.
    Dim objFS as Object
    Dim blnExits as boolean
    Set objFS = CreatObject("Scripting.FileSystemObject")
    blnExist = objFS.FileExists(strPath)

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2001
    Location
    Australia
    Posts
    10

    Check "FileExists" before continue with procedure.

    Thankyou very Much - Cheers
    Tony Dow

  5. #5
    Matthew Gates
    Guest
    Originally posted by John Yingling
    Example.
    Dim objFS as Object
    Dim blnExits as boolean
    Set objFS = CreatObject("Scripting.FileSystemObject")
    blnExist = objFS.FileExists(strPath)

    You should use what VB has or API before considering to use an Object. That code will take a bit longer than what crptcblade has posted since it has to create the FSO object and all .

    VB is a first resort.
    API is a second.
    If all else fails, FSO may be a final resort.
    But FSO is just an easier way of programming, I believe.
    Everything that is done in FSO can be done with API.

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