• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Deleting Log File

I have a function that creates a log file. It works fine, but I would like to  have it deleted when my application is killed. Any suggestions? I included my Sub:

Public Sub Log(LogFilename As String, LogInfo As String)
   Dim File As Integer
   
   
   'Write to file
   File = FreeFile
   Open LogPath + "C:\LOG\" & LogFilename For Append As #File
   Print #intFile, LogInfo

   Close #intFile

   Exit Sub


End Sub


'Thanks in advance
0
LeeHenry
Asked:
LeeHenry
  • 3
  • 3
  • 2
2 Solutions
 
EDDYKTCommented:
use kill

ie

kill "C:\LOG\" & LogFilename
0
 
EDDYKTCommented:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
kill "your log file name"
End Sub
0
 
LeeHenryAuthor Commented:
Thanks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PBuckCommented:
A couple of suggestions.  

First off, always wrap the Kill function with a Dir$() otherwise an error will be produced if the file has not been created.

If Dir$("C:\LOG\" & LogFilename, vbNormal) <> vbNullString then
   Kill "C:\LOG\" & LogFilename
End if

Secondly, are you sure you are not interested in keeping a logfile history but maintain the filesize?  What is the point of a log file if you can not view it after your application is complete?  Some people create and append to log files but keep it small (lets say around 100K) for future reference.  Some people even go as far as keeping one prior version of the log file.

Such as, applog.txt becomes 100K in size -> so FileCopy it to applog.bak and Kill applog.txt and start over with applog.txt ?

Just a suggestion.  Hope this helps.
0
 
LeeHenryAuthor Commented:
PBuck,

Thanks for the suggestion. I am interested in keeping the file at 100K. How is this done?

Thanks.
0
 
EDDYKTCommented:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If (FileLen("your log file name") > 100 * 1024) Then
    Kill "your log file name" & "old"
    Name "your log file name" As "your log file name" & "old"
End If
End Sub
0
 
PBuckCommented:
Dim fNo           As Integer
Dim lFileSize     As Long

Const FILE_BACKUP As String = "applog.bak"

   If Dir$("C:\LOG\" & LogFilename, vbNormal) <> vbNullString Then
      lFileSize = FileLen("C:\LOG\" & LogFilename)
      If lFileSize > 99999 Then
         FileCopy "C:\LOG\" & LogFilename, "C:\LOG\" & FILE_BACKUP
         Kill "C:\LOG\" & LogFilename
      End If
   End If
   
   fNo = FreeFile
   
   Open "C:\LOG\" & LogFilename For Append As fNo
      Print #fNo, strMessage
   Close fNo

'Here is a sample - hope this helps
0
 
LeeHenryAuthor Commented:
Thanks so much
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now