Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now