Jump to content

How to limit my AutoIt program log file size?


 Share

Recommended Posts

Hi everyone,

I wrote two programs with AutoIt, one of them runs silently and synchronizes a directory between two computers, after a series of checks. It then writes some log lines to a text log file.

I'm wondering if anyone has an idea on how to limit this log file size, I mean, discarding the oldest lines after some pre-determined file size, so the log file does not grow very much. I'm a little rusty on my programming skills, hehehh... Would appreciate any directions.

Thanks,

MarceloP

Link to comment
Share on other sites

$maxlines = 120
$file = @scriptdir&"/test.txt"

toolong($file,$maxlines)


func toolong($filei,$maxlines)
    if lines($filei) > $maxlines then 
        $a = filetoarray($filei)
        $file = FileOpen($filei, 2)
        for $i = $a[0]-$maxlines to $a[0]
            FileWriteLine($filei,$a[$i] & @crlf)
        next
        FileClose($filei)
    endif
endfunc

func filetoarray($file)
    $count = lines($file)
    dim $ar[$count+1]
    $ar[0]=$count
    $file = FileOpen($file, 0)
    $i = 1
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $ar[$i] = $line
        $i += 1
    Wend
    FileClose($file)
    return $ar
EndFunc

func lines($file)
    $file = FileOpen($file, 0)
    $i = 0
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $i += 1
    Wend
    FileClose($file)
    return $i
endfunc

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...