Solved

check for file

Posted on 2002-07-02
3
139 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

695 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