Sign in to follow this  
Followers 0
SpookMeister

Cleanup

1 post in this topic

Compiled as cleanup.exe

I'm calling it through scheduled tasks to do periodic maintenance on folders that gather a lot of log files etc.

The _FileSearch and _FileSearchUtil functions are Larry's (thanks man)

-Spook

#include <date.au3>
#cs ----- CleanUp ------
    A command line executable that deletes files in a given folder
    that are over a given age. 3/23/06
    
    Example:
    C:\cleanup.exe "c:\test\*.*" 60 /R /D
    
#ce --------------------
If $CmdLine[0] = 0 Then help()
If $CmdLine[1] = "/?" Then help()
If $CmdLine[1] = "?" Then help()
If $CmdLine[0] = 1 And StringInStr($CmdLine[1], ":\") Then showit($CmdLine[1], 30, 0)
If $CmdLine[0] = 2 And StringInStr($CmdLine[1], ":\") And StringIsInt($CmdLine[2]) Then showit($CmdLine[1], $CmdLine[2], 0)
If $CmdLine[0] = 2 And StringInStr($CmdLine[1], ":\") And StringInStr($CmdLine[2], "/R") Then showit($CmdLine[1], 30, 1)
If $CmdLine[0] = 3 And StringInStr($CmdLine[1], ":\") And StringIsInt($CmdLine[2]) And StringInStr($CmdLine[3], "/R") Then showit($CmdLine[1], $CmdLine[2], 1)
If $CmdLine[0] = 2 And StringInStr($CmdLine[1], ":\") And StringInStr($CmdLine[2], "/D") Then workit($CmdLine[1], 30, 0)
If $CmdLine[0] = 3 And StringInStr($CmdLine[1], ":\") And StringIsInt($CmdLine[2]) And StringInStr($CmdLine[3], "/D") Then workit($CmdLine[1], $CmdLine[2], 0)
If $CmdLine[0] = 3 And StringInStr($CmdLine[1], ":\") And StringInStr($CmdLine[2], "/R") And StringInStr($CmdLine[2], "/D") Then workit($CmdLine[1], 30, 1)
If $CmdLine[0] = 4 And StringInStr($CmdLine[1], ":\") And StringIsInt($CmdLine[2]) And StringInStr($CmdLine[3], "/R") And StringInStr($CmdLine[4], "/D") Then workit($CmdLine[1], $CmdLine[2], 1)

help()

Func showit($path_and_mask, $target_age, $recursive)
    $a = _FileSearch($path_and_mask, $recursive)
    If $a[0] > 0 Then
        $list=""
        For $i = 1 To $a[0]
            $file_age = days_since_modified($a[$i])
            If $file_age > $target_age Then
                $list=$list & $a[$i] & @CRLF
            EndIf
        Next
    EndIf
    MsgBox(0, "old files", $list)
    Exit
EndFunc  ;==>workit

Func workit($path_and_mask, $target_age, $recursive)
    $a = _FileSearch($path_and_mask, $recursive)
    If $a[0] > 0 Then
        For $i = 1 To $a[0]
            $file_age = days_since_modified($a[$i])
            If $file_age > $target_age Then
                FileDelete($a[$i])
            EndIf
        Next
    EndIf
    Exit
EndFunc  ;==>workit

Func help()
    $msg = "Cleanup is a command line utility that deletes files older than a given age." & @CRLF & _
            @CRLF & "Syntax:" & @CRLF & _
            @CRLF & "CLEANUP   ""path_and_mask""   [days_old]   [/R]  [/D]" & @CRLF & _
            @CRLF & "  path_and_mask    I.E. C:\folder\sub-folder\*.* (use quotes if there are spaces)" & _
            @CRLF & "  days_old age of files to delete (default is 30)" & _
            @CRLF & "  /R       recursive (default is non-recursive)" & _
            @CRLF & "  /D       delete the files, without /D it just shows a list matching the mask" & @CRLF & _
            @CRLF & "Examples:" & _
            @CRLF & "CLEANUP ""C:\test fol\*.txt"" 60 /R /D" & _
            @CRLF & "CLEANUP C:\test\*.log /R /D" & _
            @CRLF & "CLEANUP C:\test\*.* 25 /D" & @CRLF
    MsgBox(0, "Cleanup Help", $msg)
    Exit
EndFunc  ;==>help

Func days_since_modified($full_path_to_file)
    $FileInfo = FileGetTime($full_path_to_file, 0, 0)
    $formated_file_date = $FileInfo[0] & "/" & $FileInfo[1] & "/" & $FileInfo[2]
    $formated_current_date = @YEAR & "/" & @MON & "/" & @MDAY
    $date_diff = _DateDiff('D', $formated_file_date, $formated_current_date); needs <date.au3>
    Return $date_diff
EndFunc  ;==>days_since_modified

Func _FileSearch($szMask, $nOption)
    $szRoot = ""
    $hFile = 0
    $szBuffer = ""
    $szReturn = ""
    $szPathList = "*"
    Dim $aNULL[1]
    
    If Not StringInStr($szMask, "\") Then
        $szRoot = @ScriptDir & "\"
    Else
        While StringInStr($szMask, "\")
            $szRoot = $szRoot & StringLeft($szMask, StringInStr($szMask, "\"))
            $szMask = StringTrimLeft($szMask, StringInStr($szMask, "\"))
        WEnd
    EndIf
    If $nOption = 0 Then
        _FileSearchUtil($szRoot, $szMask, $szReturn)
    Else
        While 1
            $hFile = FileFindFirstFile($szRoot & "*.*")
            If $hFile >= 0 Then
                $szBuffer = FileFindNextFile($hFile)
                While Not @error
                    If $szBuffer <> "." And $szBuffer <> ".." And _
                            StringInStr(FileGetAttrib($szRoot & $szBuffer), "D") Then _
                            $szPathList = $szPathList & $szRoot & $szBuffer & "*"
                    $szBuffer = FileFindNextFile($hFile)
                WEnd
                FileClose($hFile)
            EndIf
            _FileSearchUtil($szRoot, $szMask, $szReturn)
            If $szPathList == "*" Then ExitLoop
            $szPathList = StringTrimLeft($szPathList, 1)
            $szRoot = StringLeft($szPathList, StringInStr($szPathList, "*") - 1) & "\"
            $szPathList = StringTrimLeft($szPathList, StringInStr($szPathList, "*") - 1)
        WEnd
    EndIf
    If $szReturn = "" Then
        $aNULL[0] = 0
        Return $aNULL
    Else
        Return StringSplit(StringTrimRight($szReturn, 1), "*")
    EndIf
EndFunc  ;==>_FileSearch

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)
    $hFile = FileFindFirstFile($ROOT & $MASK)
    If $hFile >= 0 Then
        $szBuffer = FileFindNextFile($hFile)
        While Not @error
            If $szBuffer <> "." And $szBuffer <> ".." Then _
                    $RETURN = $RETURN & $ROOT & $szBuffer & "*"
            $szBuffer = FileFindNextFile($hFile)
        WEnd
        FileClose($hFile)
    EndIf
EndFunc  ;==>_FileSearchUtil

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

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