[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Test for existance of File

Posted on 2000-01-15
9
Medium Priority
?
226 Views
Last Modified: 2010-05-02
How can I find out if a file exists before I try to open it?  Using vb5
0
Comment
Question by:donb1
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2355728
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
 
LVL 71

Expert Comment

by:Éric Moreau
ID: 2355737
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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2355777
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:donb1
ID: 2355841
vbmaster's comment is the easiest and it works fine.
0
 
LVL 7

Accepted Solution

by:
Vbmaster earned 200 total points
ID: 2355917
Nice ;)
0
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2355958
Just curious if 'Microsoft Scripting Runtime' is available in VB5?
0
 

Author Comment

by:donb1
ID: 2355966
Couldn't find 'Microsoft Scripting Runtime'
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2355972
Nope, not with VB5.
0
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2355974
Thanks!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question