Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

check for file

Posted on 2002-07-02
3
Medium Priority
?
141 Views
Last Modified: 2010-05-02
hello,

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

thanks
0
Comment
Question by:kkarnez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

610 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