Sign in to follow this  
Followers 0
Jcold8

Text log File Control

3 posts in this topic

Hi, I'm trying to make a text file that logs when a script was run and have it also delete some lines so that the text file doesn't keep growing. I just want to keep like 5 lines in there instead of never ending.

I used the _filewriteline and followed the help file to make it overwrite the first couple lines but it doesnt seem to be working.

$file = FileOpen("logfile.txt", 1)

; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWrite($file, "Script Successful....")
#include <Date.au3>
FileWrite($file, _DateTimeFormat( _NowCalc(),2))
FileWrite($file, "....")
FileWrite($file, _DateTimeFormat( _NowCalc(),3) & @CRLF)

#include <File.au3>
_FileWriteToLine($file, 1, "", 1)
_FileWriteToLine($file, 2, "", 1)
_FileWriteToLine($file, 3, "", 1)

FileClose($file)

Share this post


Link to post
Share on other sites



#include <Date.au3>
#include <File.au3>
If FileExists("logfile.txt") = 0 Then
    FileWrite("logfile.txt", "")
EndIf
If _FileCountLines("logfile.txt") < 5 Then
    FileWrite("logfile.txt", _DateTimeFormat(_NowCalc(), 3) & @CRLF)
Else
    $replace = StringReplace(FileRead("logfile.txt"), FileReadLine("logfile.txt", 1) & @CRLF, "")
    FileDelete("logfile.txt")
    FileWrite("logfile.txt", $replace & _DateTimeFormat(_NowCalc(), 3) & @CRLF)
EndIf

Share this post


Link to post
Share on other sites

#include <Date.au3>
#include <File.au3>
If FileExists("logfile.txt") = 0 Then
    FileWrite("logfile.txt", "")
EndIf
If _FileCountLines("logfile.txt") < 5 Then
    FileWrite("logfile.txt", _DateTimeFormat(_NowCalc(), 3) & @CRLF)
Else
    $replace = StringReplace(FileRead("logfile.txt"), FileReadLine("logfile.txt", 1) & @CRLF, "")
    FileDelete("logfile.txt")
    FileWrite("logfile.txt", $replace & _DateTimeFormat(_NowCalc(), 3) & @CRLF)
EndIf
Thanks for the help. This only will display one of the things i want in it.

It shows up as "script successful....7/12/071:23PM" in my file.

So I would like it to replace the whole line, I messed around with what you gave me but its adding them after 5 lines instead of replacing.

Any ideas.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0