We help IT Professionals succeed at work.

How to housekeep log file

Bianchi928
Bianchi928 asked
on
I have a logfile that needs to be housekept. By that I mean to keep the last 10 lines.
logfile.txt
Comment
Watch Question

This script will read your text file into an array, then overwrite the contents of the file with the last 10 lines. Just edit the location of the text file.
Now run this script as a scheduled task or similar if it needs to happen frequently
 
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\support\logfile.txt", 1)
Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFile.ReadLine
     i = i + 1
Loop
objFile.Close
Set objFileWrite = objFSO.OpenTextFile("c:\support\logfile.txt", 2)
For l =  (UBound(arrFileLines))-9 to UBound(arrFileLines)
	'wscript.echo arrFileLines(l)
	objFileWrite.WriteLine arrFileLines(l)
Next
objFileWrite.Close

Open in new window

Author

Commented:
Perfect ..Thanks a lot
Cheers