rsmedude

Delete log files based on date (AGE)

9 posts in this topic

#1 ·  Posted

Good Afternoon everyone,

Ok so here is the project I was working on. I have a program running that shows me train movement. (I am into trains and model railroading.) You can learn more about the ATCS program here. I have this program dumping a screen capture of the territory I want to monitor to a webpage that I have. The screen capture stuff is all done natively in the program. Unfortunately every so often it likes to crash. Instead find out when I am already out by the tracks I wrote a script (with some help) that checks if the program is running. If it is not it starts it back up. I also have it writing its own log so that I can check if there was something "strange" or "unexpected" (would be the better word) going on. Recently I have added lines of code that remove old entries from that log after so long if it meets specific criteria. I again got a little help with that . Now I would like to add a few lines that will delete old daily logs from the ATCS program itself. I want to do this so that I can help avoid the program bloating out of control. As near as I can figure it has no "If log is X old then delete" built into its native programming. So now I want to add it to my script. I have been spinning my tires on this for the entire weekend now. I have decided I want to ask for help again.

Here are my requirements.

Delete *.log files from a specified directory on the computer.

Files are equal to or older than 30 days.

The script should not generate any pop ups. MUST RUN AUTONOMOUSLY!

I found this on the message boards: Delete old Files

I like this coding from that post:

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

    $files = _FileListToArray("Your path here", "*.ext", 2) ;create an array of files in the specified folder
    $date=@YEAR&"/"&@MON&"/"&@MDAY;getting the current date
    $newdate=_DateAdd("D",-120,$date) ;adding -120 days (subtract 120 days)
    $formatdate=StringSplit($newdate,"/") ;removing the /
    $newdate=$formatdate[1]&$formatdate[2]&$formatdate[3]&@HOUR&@MIN&@SEC ;putting the date back together in a format easily compared to the FileGetTime func
    If IsArray($files) Then ;Making sure an array was created
        For $i = 1 To UBound($files) - 1 ;Loop through all the files found
            $aTime = FileGetTime("Your path here" & $files[$i], 1, 1) ;get the creation time of the file
            If $aTime < $newdate Then ;check to see if creation time is older than 120 days
                FileDelete($files[$i]) ;delete the file if it is
            EndIf
        Next
    EndIf

I copied it and changed the appropriate areas to meet my needs. Eg. "Your path here" to "C:\_TEMP".

I also changed the 120 days to 30 days.

Here is my copy of this coding that I manipulated:

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

    $files = _FileListToArray("C:\_TEMP", "*.log", 2) ;create an array of files in the specified folder

    $date = @YEAR&"/"&@MON&"/"&@MDAY;getting the current date

    $newdate = _DateAdd("D",-30,$date) ;adding -30 days (subtract 30 days)

    $formatdate = StringSplit($newdate,"/") ;removing the /

    $newdate = $formatdate[1]&$formatdate[2]&$formatdate[3]&@HOUR&@MIN&@SEC ;putting the date back together in a format easily compared to the FileGetTime func

    If IsArray($files) Then ;Making sure an array was created

      For $i = 1 To UBound($files) - 1 ;Loop through all the files found

            $aTime = FileGetTime("C:\_TEMP" & $files[$i], 1, 1) ;get the creation time of the file

            If $aTime < $newdate Then ;check to see if creation time is older than 120 days

                FileDelete($files[$i]) ;delete the file if it is

            EndIf

        Next

    EndIf

My edits do not make it error that I know of. It just doesn't do anything. It opens and closes and that's it. So my question is what am I missing?

 

Thank you in advance for any and all help.

Respectfully,

James


[font="Arial"]James D. Williams[/font]

Share this post


Link to post
Share on other sites



#2 ·  Posted

You have a few things here that are going against your results. I have copied your code, added some testing functions so I can show you what is going wrong here in this first snippet.

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

#include <Array.au3>

    $files = _FileListToArray("C:\_TEMP", "*.log", 2) ;create an array of files in the specified folder
_ArrayDisplay($files)
    ;including this function you can see no array is being created. Reason is because in the thirdparameter it 
    ;requires '1' to return files, or searches for Directories with those names if '2' is the third parameter.
    ;We need to change that thrid parameter to '1')
    
    $date = @YEAR&"/"&@MON&"/"&@MDAY;getting the current date

    $newdate = _DateAdd("D",-30,$date) ;adding -30 days (subtract 30 days)
MsgBox('', 'Is it being converted?', '   $date=' & $date & @CRLF & '$newdate=' & $newdate)
    ;The problem here is that _DateAdd is looking for a specific time format to convert, not any format will do
    ;This is easily correct by calling the current date with _NowCalc which will provide the date and time format
    ;that _DateAdd() requires.
    
    $formatdate = StringSplit($newdate,"/") ;removing the /
    ;These line of code will nolonger be needed utilizing a few functions nested together
    ;in this next line of code.
    
    $newdate = $formatdate[1]&$formatdate[2]&$formatdate[3]&@HOUR&@MIN&@SEC ;putting the date back together in a format easily compared to the FileGetTime func

    If IsArray($files) Then ;Making sure an array was created

      For $i = 1 To UBound($files) - 1 ;Loop through all the files found

            $aTime = FileGetTime("C:\_TEMP" & $files[$i], 1, 1) ;get the creation time of the file
                ;The path also needs a file/directory seperator
            If $aTime < $newdate Then ;check to see if creation time is older than 120 days

                FileDelete($files[$i]) ;delete the file if it is

            EndIf

        Next

    EndIf

Now, I have modified your script a little while trying to keep your format as much as possible. I do believe this will work better for you.

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

    $files = _FileListToArray(@DesktopDir, "*.txt", 1) ;create an array of files in the specified folder

    $date = _NowCalc();getting the current date

    $newdate = _DateAdd("D",-30,$date) ;adding -30 days (subtract 30 days)

    $newdate = StringStripWS(StringReplace(StringReplace($newdate,':',''),'/',''), 8) ;putting the date back together in a format easily compared to the FileGetTime func

    If IsArray($files) Then ;Making sure an array was created

      For $i = 1 To UBound($files) - 1 ;Loop through all the files found

            $aTime = FileGetTime(@DesktopDir & '\' & $files[$i], 1, 1) ;get the creation time of the file

            If $aTime < $newdate Then ;check to see if creation time is older than 120 days

                FileDelete($files[$i]) ;delete the file if it is

            EndIf

        Next

    EndIf

I hope this helps, and please let me know if it does. 

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

#3 ·  Posted

14 minutes ago, Realm said:

You have a few things here that are going against your results. I have copied your code, added some testing functions so I can show you what is going wrong here in this first snippet.

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

#include <Array.au3>

    $files = _FileListToArray("C:\_TEMP", "*.log", 2) ;create an array of files in the specified folder
_ArrayDisplay($files)
    ;including this function you can see no array is being created. Reason is because in the thirdparameter it 
    ;requires '1' to return files, or searches for Directories with those names if '2' is the third parameter.
    ;We need to change that thrid parameter to '1')
    
    $date = @YEAR&"/"&@MON&"/"&@MDAY;getting the current date

    $newdate = _DateAdd("D",-30,$date) ;adding -30 days (subtract 30 days)
MsgBox('', 'Is it being converted?', '   $date=' & $date & @CRLF & '$newdate=' & $newdate)
    ;The problem here is that _DateAdd is looking for a specific time format to convert, not any format will do
    ;This is easily correct by calling the current date with _NowCalc which will provide the date and time format
    ;that _DateAdd() requires.
    
    $formatdate = StringSplit($newdate,"/") ;removing the /
    ;These line of code will nolonger be needed utilizing a few functions nested together
    ;in this next line of code.
    
    $newdate = $formatdate[1]&$formatdate[2]&$formatdate[3]&@HOUR&@MIN&@SEC ;putting the date back together in a format easily compared to the FileGetTime func

    If IsArray($files) Then ;Making sure an array was created

      For $i = 1 To UBound($files) - 1 ;Loop through all the files found

            $aTime = FileGetTime("C:\_TEMP" & $files[$i], 1, 1) ;get the creation time of the file
                ;The path also needs a file/directory seperator
            If $aTime < $newdate Then ;check to see if creation time is older than 120 days

                FileDelete($files[$i]) ;delete the file if it is

            EndIf

        Next

    EndIf

Now, I have modified your script a little while trying to keep your format as much as possible. I do believe this will work better for you.

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

    $files = _FileListToArray(@DesktopDir, "*.txt", 1) ;create an array of files in the specified folder

    $date = _NowCalc();getting the current date

    $newdate = _DateAdd("D",-30,$date) ;adding -30 days (subtract 30 days)

    $newdate = StringStripWS(StringReplace(StringReplace($newdate,':',''),'/',''), 8) ;putting the date back together in a format easily compared to the FileGetTime func

    If IsArray($files) Then ;Making sure an array was created

      For $i = 1 To UBound($files) - 1 ;Loop through all the files found

            $aTime = FileGetTime(@DesktopDir & '\' & $files[$i], 1, 1) ;get the creation time of the file

            If $aTime < $newdate Then ;check to see if creation time is older than 120 days

                FileDelete($files[$i]) ;delete the file if it is

            EndIf

        Next

    EndIf

I hope this helps, and please let me know if it does. 

Realm

Hello Realm,

 

Thank you for the response and help. I see where the issues were now that someone pointed them out to me. I do have questions with the coding you put together or rather revamped.

1. Do we not need the #include <Array.au3> header at the top of the coding anymore?
2. Where you inserted @DesktopDir shouldn't that be the specific directory I want to work with? I moved a few "test" files into a temp folder so I wouldn't botch up the working (production if you will) program. Ultimately I want to make sure I point it to the location the program is installed at so that it does its function to the production area.

Sorry to be not in the know. I am still learning how to do these things for my little world over here.

Respectfully,
James


[font="Arial"]James D. Williams[/font]

Share this post


Link to post
Share on other sites

#4 ·  Posted

Hello James,

1. You will not need the  #include <Array.au3>  in the last example. I included in the first example in order to use the _ArrayDisplay() function to show no array was being populated.

2. I'm sorry about the @DesktopDir confusion, I meant to change that back to your directory call before I posted and forgot. I used @DesktopDir for my own testing purposes since I was certain I had text files both under and over 30 days there. You can freely change the directory and the .txt back to .log or whatever file extension you choose.

Here is the version I should have posted:

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

    $files = _FileListToArray("C:\_TEMP", "*.log", 1) ;create an array of files in the specified folder

    $date = _NowCalc();getting the current date

    $newdate = _DateAdd("D",-30,$date) ;adding -30 days (subtract 30 days)

    $newdate = StringStripWS(StringReplace(StringReplace($newdate,':',''),'/',''), 8) ;putting the date back together in a format easily compared to the FileGetTime func

    If IsArray($files) Then ;Making sure an array was created

      For $i = 1 To UBound($files) - 1 ;Loop through all the files found

            $aTime = FileGetTime("C:\_TEMP" & '\' & $files[$i], 1, 1) ;get the creation time of the file

            If $aTime < $newdate Then ;check to see if creation time is older than 120 days

                FileDelete($files[$i]) ;delete the file if it is

            EndIf

        Next

    EndIf

I hope this helps, and feel free to ask any other questions you might have about this script or any other script.

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

#5 ·  Posted

Good Morning Realm,

 

No worries on the confusion. I am learning as I go. I just wanted to double check before I either broke it before I fixed it or deleted a bunch of stuff of the computer that I really didn't want to!

 

Respectfully,

James


[font="Arial"]James D. Williams[/font]

Share this post


Link to post
Share on other sites

#6 ·  Posted

OK so I tried this and it did not delete anything. I am a little confused now. I looked at the properties of the first file and here is the date information Windows is reporting.

Created: Tuesday, January, 17, 2017, 2:00:10 PM

Modified: Monday, April, 10, 2015, 10:15:24 PM

Accessed: Tuesday, January, 17, 2017, 2:00:10 PM

 

So I am not sure why its not deleting these files.

 

Respectfully,

James

 


[font="Arial"]James D. Williams[/font]

Share this post


Link to post
Share on other sites

#7 ·  Posted

If you are able to write your log files to a sub-folder named 'Today', then you could schedule something like this to run daily right after midnight and it will backup your log files to a daily sub-folder.

;=============================================================
; create new folder and delete sub-folders > 6 days old
;=============================================================
#include<Date.au3>

$yesterdayfolder = StringReplace(_DateAdd('d', -1, _NowCalcDate()), "/", "")

If Not FileExists(@ScriptDir & "\" & $yesterdayfolder) Then ;create folder with yesterday's date if it doesn't exist
    DirCreate(@ScriptDir & "\" & $yesterdayfolder)
    For $x = -10 To -6 Step 1 ; use for loop in case this doesn't run for a few days.  It will still delete old folders.
        $DeleteFolder = StringReplace(_DateAdd('d', $x, _NowCalcDate()), "/", "")
        DirRemove(@ScriptDir & "\" & $DeleteFolder, 1) ;removes folder and all sub-folders and files
    Next
    FileMove(@ScriptDir & "\Today\*.Log",@ScriptDir & "\" & $yesterdayfolder) ;moves all .log files in the today folder to the folder with yesterday's date
EndIf

This will just move the files from the 'Today' sub-folder into a different sub-folder with yesterday's date as the folder name and automatically delete folders that are older than x number of days (in this case, 6 days).

Share this post


Link to post
Share on other sites

#8 ·  Posted

Hello James,

I never tested actually deleting a file or I might have noticed this:

Change this line of code:

FileDelete($files[$i]) ;delete the file if it is

to this:

FileDelete("C:\_TEMP" & '\' & $files[$i]) ;delete the file if it is

That should actually delete the files for you....if not let me know.


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

#9 ·  Posted

Another way:

#include <Date.au3>
#Include <File.au3>
Local $aFileTime
Local $aFileList = _FileListToArrayRec(EnvGet("SystemDrive") & "\_TEMP", "*.log", 1, 0, 0, 2) ;~ Create an array of files in the specified folder with full path
    If @error Then Exit ;~ Exit if there was an error creating the Array
For $i = 1 To $aFileList[0] ;~ Loop through all the files found
    $aFileTime = FileGetTime($aFileList[$i], 1, 0) ;~ Get the creation time of the file as an array
    If _DateDiff("d", $aFileTime[0] & "/" & $aFileTime[1] & "/" & $aFileTime[2], _NowCalc()) >= 120 Then ;~ Check to see if creation time is greater or equal to 120 days old
        FileDelete($aFileList[$i]) ;~ Delete the file
    EndIf
Next

 

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