Solved

How to check if a file exists?

Posted on 2002-05-30
2
167 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
[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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
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…

734 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