Jump to content
Sign in to follow this  
Ambient

Triming a Logfile Problem

Recommended Posts

Ambient

Hi I wonder can someone help me with this I have been trying to trim logfiles b reading number of days to keep from an ini file. Anyway the logfile is being deleted after specified number of days and then starts to append again as I would like itto but then it deletes every time the script is run as if the original timestamp is being read, code below.

;Lets check that the logfile is not Greater than the number of days specified in the ini if it is delete it

$t = FileGetTime($logfilepath , 1)

If Not @error Then

$yyyymd = $t[0] & "/" & $t[1] & "/" & $t[2]

;MsgBox(0, "Logfile Created date : ", $yyyymd & " " & _NowCalcDate())

$iDateCalc = _DateDiff( 'd',$yyyymd,_NowCalc())

;MsgBox(0, "Logfile & logdays added : ", $iDateCalc)

if $iDateCalc >= $logdays Then

filedelete($logfilepath)

_FileWriteLog($logfilepath, "Deleted old log as it was > = " & $logdays & " Old")

endif

Share this post


Link to post
Share on other sites
zorphnog

Use _NowCalcDate to get the correct date format for the _DateDiff call - i.e.

$iDateCalc = _DateDiff( 'D',$yyyymd,_NowCalcDate())

Share this post


Link to post
Share on other sites
Ambient

Use _NowCalcDate to get the correct date format for the _DateDiff call - i.e.

$iDateCalc = _DateDiff( 'D',$yyyymd,_NowCalcDate())
Thanks I 'll try that and let you know!

Share this post


Link to post
Share on other sites
Ambient

Sorry tried this and still doing the same, I checked the file and the created, modified and accessed dates are all the same 9:55 today. My view is the script should now append to the logfile until its 3 days old but it does it deletes it every time. This script runs every 10 mins.

Share this post


Link to post
Share on other sites
zorphnog

Well I don't know what to tell you without seeing the full script. Do you know what the value of $iDateCalc is each time the file is being deleted?

Share this post


Link to post
Share on other sites
Ambient

The only piece of the code thats significant is the piece posted. $iDateCalc =6

What appears to be happening is this when the script is first run it creates a file e.g. logfile.log

This script runs every 10 minutes and the posted code is at the start of the script. Anyway the script checks the date and if the date on the file is > than $logdays which in this case is set to 3 then it deletes the logfile (this appears to be happening) I then expect the logfile to recreate at the point below:

_FileWriteLog($logfilepath, "Deleted old log as it was " & $iDateCalc & " days" & " Old")

This also appears to be happening and the timestamp on the file seems to be changed. The problem is this when the script runs 10 mins later the value in $iDateCalc is still 6 so it then deletes the file again even though the file isn't 6 days old or > 3 . Its as if its holding on to the original time stamp somewhere? I am considering changing direction and making it delete when file gets to certain size instead, although I would like to get to the bottom of this. Thanks for your input anyway it was very helpful.

Edited by Ambient

Share this post


Link to post
Share on other sites
zorphnog

Well the code works fine for me. What I meant by showing the full script is that you need to show what all the variables you reference are set to, such as $logfilepath and $logdays. You may not think its relevant, but its impossible to troubleshoot without the full picture. The only other thing I would suggest is to try putting a Sleep command after FileDelete to allow for a little extra time for the file to be deleted. You shouldn't need it, but its probably worth a try.

Are you checking the file times in explorer or a command prompt to ensure that they are actually changing?

Here's the code I used that works fine:

#include <File.au3>
#include <Date.au3>

Global $iMaxLogAge = 3, $aFileTime, $sFilename = @ScriptDir & "\my.log"

$aFileTime = FileGetTime($sFilename, 1)
If Not @error Then
    $sFileTime = $aFileTime[0] & "/" & $aFileTime[1] & "/" & $aFileTime[2]
    ConsoleWrite("-> FileGetTime: " & $sFileTime & @TAB & "_NowCalcDate: " & _NowCalcDate() & @LF)
    $iDateDiff = _DateDiff("D", $sFileTime, _NowCalcDate())
    ConsoleWrite("-> _DateDiff: " & $iDateDiff & @LF)
    
    If $iDateDiff >= $iMaxLogAge Then
        If FileDelete($sFilename) Then
            ConsoleWrite("-> Deleted file: " & $sFilename & @LF)
            _FileWriteLog($sFilename, "Deleted old log as it was " & $iDateDiff & " days old.")
        EndIf
    Else
        ConsoleWrite("-> File is current" & @LF)
    EndIf
EndIf

Share this post


Link to post
Share on other sites
Ambient

This is still behaving the same way mine did e.g What appears to be happening is this the logfile delete occurs and it writes to the logfile again i.e. it drops in here

If FileDelete($sFilename) Then

ConsoleWrite("-> Deleted file: " & $sFilename & @LF)

_FileWriteLog($sFilename, "Deleted old log as it was " & $iDateDiff & " days old.")

EndIf

It then runs through the rest of the script writting to the log as normal. When I check the date the dates are all fine today (created,modified, accessed) . Anyway the next time the script runs you would would assume that it would n't delete as the logfile is dated today but it does. So now everytime the script runs it deletes its as if it still sees the old date somehow? I really can't figure it out.

Edited by Ambient

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.