Solved

How to verify the existence of the file and delete it?

Posted on 2011-03-21
3
267 Views
Last Modified: 2012-05-11
Hi,
can you help me with my macro to delete a text file? I need to erase the file test.log (if exist) in the location of the system folder "%TEMP%\test.log".
To be precise: "%TEMP%" = Environ("TEMP")
Below is the code you need to edit.

Thanks.

Regards,
TM
Sub DeleteFile(ByVal FileToDelete As String)
Dim aFile As String
sTemp = Environ("TEMP")
aFile =  '<-You need to set the path to the file (test.log), it can be deleted. 
If Len(Dir$(aFile)) > 0 Then
     Kill aFile
End If

Open in new window

0
Comment
Question by:Thomas_Meyer
  • 2
3 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35183130
Seems like you already have it ... all but

Chris
Sub DeleteFile(ByVal FileToDelete As String)
Dim aFile As String
sTemp = Environ("TEMP")
aFile =  stemp & "\test.log" 
If Len(Dir$(aFile)) > 0 Then
     Kill aFile
End If

Open in new window

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 35183156
Not sure what filetodelete is but if it a string for the filename in the temp folder then you would use:

Note also stemp can be deleted and replace:

sTemp = Environ("TEMP")
aFile =  stemp & "\" & FileToDelete
with
aFile =  Environ("TEMP") & "\" & FileToDelete

Chris
Sub DeleteFile(ByVal FileToDelete As String)
Dim aFile As String
sTemp = Environ("TEMP")
aFile =  stemp & "\" & FileToDelete
If Len(Dir$(aFile)) > 0 Then
     Kill aFile
End If

Open in new window

0
 

Author Closing Comment

by:Thomas_Meyer
ID: 35183227
Thanks for your help
TM
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Embed scroll excel chart and data in powerpoint 9 70
Automatically report prepared in excel by VBA 5 32
Excel Calculation 4 44
Turn date into age 17 31
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article will show you how to use shortcut menus in the Access run-time environment.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

765 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