Solved

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

Posted on 2011-03-21
3
268 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you how to use shortcut menus in the Access run-time environment.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

726 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