?
Solved

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

Posted on 2011-03-21
3
Medium Priority
?
270 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
[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
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 2000 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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Suggested Courses

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