Solved

check for file

Posted on 2002-07-02
3
135 Views
Last Modified: 2010-05-02
hello,

can anyone tell me, how can check if a file exists?

thanks
0
Comment
Question by:kkarnez
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 7124643
if dir("yourfile") = empty then
  'file does not exist
else
  'file does exist
end if

CHeers
0
 
LVL 5

Expert Comment

by:rpai
ID: 7124658
Dim fso as Object

Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
    debug.print filespec & " exists."
Else
    debug.print filespec & " doesn't exist."
End If
 
0
 
LVL 3

Expert Comment

by:schworak
ID: 7124716
Both of the above methods work well.

If you are only checking to see if the file exists the first method is faster than creating the scripting object then dropping it. If you are going to do a lot of file stuff then use the second method and keep the file system object around in your program so you don't need to keep recreating it.

If you are going to read from the file you can also just open it and check for an error (this is the fastest way if you are going to use the file when it exists)

on error resume next
open "yourfile" for input as #1
if err=0 then
   '
   ' your code goes here
   '
   close #1
end if
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now