How to - Check if File Exists in Excel VBA 
Author Message
 How to - Check if File Exists in Excel VBA

In Excel VBA, how do you check to see if a file exists and if so, delete it?

Don



Fri, 02 Mar 2001 03:00:00 GMT  
 How to - Check if File Exists in Excel VBA
Don,

The Dir and Kill functions can be used to check for the existence of the
file and then delete it if it exists:

    If Dir(FileName) <> "" Then Kill FileName

John



Quote:
> In Excel VBA, how do you check to see if a file exists and if so, delete
it?

> Don




Fri, 02 Mar 2001 03:00:00 GMT  
 How to - Check if File Exists in Excel VBA
Don,
This would be one approach:

Sub delfile()
If Len(Dir("c:\windows\test.txt")) > 0 Then
   Kill "c:\windows\test.txt"
   MsgBox "Test.txt has been annihilated"
Else
   MsgBox "Test.Txt never existed"
End If

End Sub

HTH,
Tom Ogilvy

Quote:
> ----------

> Posted At:         Monday, September 14, 1998 1:10 PM
> Posted To:         microsoft.public.excel.programming
> Conversation:      How to - Check if File Exists in Excel VBA
> Subject:   How to - Check if File Exists in Excel VBA

> In Excel VBA, how do you check to see if a file exists and if so,
> delete it?

> Don




Fri, 02 Mar 2001 03:00:00 GMT  
 How to - Check if File Exists in Excel VBA
Tom's answer is correct.  Just remember that using the KILL statement does
NOT put the file in the "recycle bin". The file is gone forever, never to be
recovered. (I once had an angry client who KILLed a bunch of files,
expecting them to be available for restoration from the recycle bin.  That
is NOT what KILL does!)

Cordially,
Chip Pearson
http://home.gvi.net/~cpearson/excel.htm



Quote:
>Don,
>This would be one approach:

>Sub delfile()
>If Len(Dir("c:\windows\test.txt")) > 0 Then
>   Kill "c:\windows\test.txt"
>   MsgBox "Test.txt has been annihilated"
>Else
>   MsgBox "Test.Txt never existed"
>End If

>End Sub

>HTH,
>Tom Ogilvy

>> ----------

>> Posted At: Monday, September 14, 1998 1:10 PM
>> Posted To: microsoft.public.excel.programming
>> Conversation: How to - Check if File Exists in Excel VBA
>> Subject: How to - Check if File Exists in Excel VBA

>> In Excel VBA, how do you check to see if a file exists and if so,
>> delete it?

>> Don




Fri, 02 Mar 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Excel -Checking if a file exists ( vba/wscript?)

2. Check if sheet exists in Excel with vba from foxpro

3. Check DLL-File (Exist DLL-File?)

4. Check DLL-File (Exist DLL-File?)

5. Check-DLL-File (Exist DLL-File?)

6. Excel VBA? - Check if a file is OPEN

7. checking a sheet exists using its vba name

8. How to check in VBA if a workbook exists

9. How to check in VBA if worksheet exists

10. VBA code to check if Talble exists?


 
Powered by phpBB® Forum Software © phpBB Group