Solved

Deleting Log File

Posted on 2003-12-04
8
332 Views
Last Modified: 2010-08-05
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
Comment
Question by:LeeHenry
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Accepted Solution

by:
EDDYKT earned 125 total points
ID: 9875083
use kill

ie

kill "C:\LOG\" & LogFilename
0
 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 125 total points
ID: 9875091
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
kill "your log file name"
End Sub
0
 
LVL 6

Author Comment

by:LeeHenry
ID: 9875519
Thanks
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 4

Expert Comment

by:PBuck
ID: 9875543
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
 
LVL 6

Author Comment

by:LeeHenry
ID: 9882711
PBuck,

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

Thanks.
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9882819
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
 
LVL 4

Expert Comment

by:PBuck
ID: 9882834
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
 
LVL 6

Author Comment

by:LeeHenry
ID: 9884787
Thanks so much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

691 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