Test for existance of File

How can I find out if a file exists before I try to open it?  Using vb5
donb1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MTroutwineCommented:
Here are two different ways to check.  The first one does not need require you to include any additional resources:

Public Function FileExists(ByVal FilePath As String) As Boolean
   
    ' ----------------------------------
    ' Handle Errors:
    ' ----------------------------------
    On Error Resume Next
   
    ' ----------------------------------
    ' Local Variable Declarations:
    ' ----------------------------------
    Dim lngFileNum As Long
   
    ' Trim right to the first backslash:
    If Right(FilePath, 1) = "\" Then
        ' Assign value to variable:
        FilePath = Left(FilePath, Len(FilePath) - 1)
    End If
   
    ' Open the file:
    lngFileNum = FreeFile
    Open FilePath For Input As lngFileNum
   
    ' Return value:
    ' If there was an error the file does not exist:
    If Err <> 0 Then
        FileExists = False
        Err = 0
    ' No error indicates the file exist and was opened:
    Else
        FileExists = True
    End If
       
    ' Close the file:
    Close lngFileNum
   
End Function

This one is simpilar but you need to incude a reference in your project to 'Microsoft Scripting Runtime', which is the 'scrrun.dll'.

All you need now is to add the following:

Dim fsFile          As New Scripting.FileSystemObject

fsFile.FileExists("PathofFileBeingChecked")

I started using VB at version 6.0, used VC++ before that so I am not certain if VB5 supports 'Microsoft Scripting Runtime'.

:>)
0
Éric MoreauSenior .Net ConsultantCommented:
Instead of opening the file just to see if it exists, look in the help for the Dir function. If an error is returned then the file do not exist. You can also check if a folder exist with DIR.
0
VbmasterCommented:
Function FileExist(Filename As String) As Boolean

  On Error GoTo Errorhandler
 
  Call FileLen(Filename)
  FileExist = True
  Exit Function

Errorhandler:
  FileExist = False

End Function
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

donb1Author Commented:
vbmaster's comment is the easiest and it works fine.
0
VbmasterCommented:
Nice ;)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MTroutwineCommented:
Just curious if 'Microsoft Scripting Runtime' is available in VB5?
0
donb1Author Commented:
Couldn't find 'Microsoft Scripting Runtime'
0
VbmasterCommented:
Nope, not with VB5.
0
MTroutwineCommented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.