Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting Flat Files to a Specified Size

Status
Not open for further replies.

DaveyCrockett

Programmer
Jul 15, 2003
36
US
Is there a way to limit how big a file can grow and to delete or overwrite old data. ie. I have a VB Application that is writing to a log file using APPEND. This file can get pretty big if I don't clean it up.

This is my function that writes to the file:
Public Function WriteToFile(LogFile As String, LogMsg As String)
On Error Resume Next
Open LogFile For Append As #1
Print #1, LogMsg
Close #1
End Function

I call this function as follows:
WriteToFile App.Path & "\LogFile.txt", "String that gets inserted into the file"

Would any of you know how I could limit the file to say 100k?


Thanks
 
set a reference to "Microsoft Scripting Runtime."
dim fso as FileSystemObject
dim cFile as File
set fso = new FileSystemObject
set cFile = fso.GetFile("MyFilePathandName")
if cFile.Size > 100000 then
fso.DeleteFile("MyFilePathAndName")
end if
(Recreate your file if necessary.)

That should be pretty close, off the top of my head.

Bob
 
Bob,

I tried your suggestion, but to no avail. Do you have any other suggestions?

Thanks
 
You could use the Dir() and FileLen() functions. Press F1 and see the help files on it.
 
think you can follow the following steps (simple though)

1.Before opening the file to append, check the filesize.
2. If it is = the max file size u need, delete the file.
-May be u can log this deletion itself somewhere
3. [Re]Write the File Contents

HTH
Engi

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top