Solved

How to check if a file exists?

Posted on 2002-05-30
2
158 Views
Last Modified: 2010-05-02
I want to perform a Kill Statement on a file(to delete it if a certain event occures) but I need to know before if the file exists(to avoid the error).
I tried the On Error but it doesn't work.
Any ideas?
Thank you
0
Comment
Question by:robertut
  • 2
2 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 7045447
Using the same path that would appear in the kill, just use Dir$(filename).  If it returns vbnullstring ("") then the file does not exist.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
ID: 7045449
Example:

strFilename = "C:\Temp\myfile.txt"
if Dir$(strFilename) <> vbNullString then
  kill strFilename
end if
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

705 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

21 Experts available now in Live!

Get 1:1 Help Now