Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to With line 4 57
COPY from excel to notepad 3 52
MS Access 03, TransferText, decimal places 8 68
RUNRMTCMD from AS/400 12 43
My experience with Windows 10 over a one year period and suggestions for smooth operation
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

805 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