Link to home
Start Free TrialLog in
Avatar of Thomas_Meyer
Thomas_MeyerFlag for Czechia

asked on

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

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

Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thomas_Meyer

ASKER

Thanks for your help
TM