Jump to content

Delete Files depending on their mod time


tomster82
 Share

Recommended Posts

As i promised here the script.

I´ve added another function. Now u can customise an ini file in order to change the Root folders which are checked. that´s more easyer for use.

Here the script:

; ----------------------------------------------------------------------------
; §AutoIt-Optionen setzen
; ----------------------------------------------------------------------------

AutoItSetOption("TrayIconDebug", 1)
AutoItSetOption("WinTitleMatchMode", 2)
AutoItSetOption("TrayIconHide", 0)

; ----------------------------------------------------------------------------
; §Inkludierungen
; ----------------------------------------------------------------------------

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

; ----------------------------------------------------------------------------
; §Constante Variablen
; ----------------------------------------------------------------------------
Const $MAXAGE_ALL = 30                 ; Days
Const $MAXAGE_FM  = 185            ; Days


; ----------------------------------------------------------------------------
; §Globale Variablen
; ----------------------------------------------------------------------------

Global $msg = ""
Global $i = 1 

; ----------------------------------------------------------------------------
; §Script Start
; ----------------------------------------------------------------------------


$ROOTDIR =""

While $ROOTDIR <>"NotFound"
    $ROOTDIR = IniRead("C:\Temp\config.ini", "directory" , "key" & $i, "NotFound")
    
        
    If $ROOTDIR <>"NotFound" then 
        $logfile = $ROOTDIR & "deleted_files.log"
        
        _DeleteFileRecursive($ROOTDIR, "*.*", $logfile )
    EndIf
    $i = $i+1
WEnd 

$retval = _DeleteFileRecursive($ROOTDIR,"*.*",$LOGFILE)

msgbox(0,"","Number of Files found: " & $retval)


ClipPut($msg)




Func _DeleteFileRecursive($sPath, $pattern, $logfile = "")
    Local $files = _FileListToArray($sPath,$pattern,1)
    If @error <> 0 then return -1

    Local $dirs = _FileListToArray($sPath,"*",2)
    If @error <> 0 Then Return -1

    Local $age, $nFilesFound = 0, $retval

    If IsArray($files) Then
    
        For $n = 1 to $files[0]
    
                
                
            Select
                Case StringInStr($sPath & "\","\visvip_elemente\") OR StringInStr($sPath & "\","\visvip_vdafs\") OR StringInStr($sPath & "\","modelle") 
            
                Case StringInStr($files[$n],".htaccess")
        
                Case StringInStr($sPath & "\","\fertigungsmittel\")
                    $age = _DateDiffInDays($sPath & "\" & $files[$n] )
                        If $age > $MAXAGE_FM then 
                            $nFilesFound = $nFilesFound + 1
                            _DeleteFile($sPath & "\" & $files[$n],$age)
                            
                             If $logfile <> "" then
                                _FileWriteLog($logfile, "Deleted " & $age & " days old file: " & $sPath & "\" & $files[$n])
                             EndIf
                                                    EndIf               
                Case Else
                    $age = _DateDiffInDays($sPath & "\" & $files[$n])
                        If $age > $MAXAGE_ALL then 
                            $nFilesFound = $nFilesFound + 1
                            _DeleteFile($sPath & "\" & $files[$n],$age)
                                                        If $logfile <> "" then
                                _FileWriteLog($logfile, "Deleted " & $age & " days old file: " & $sPath & "\" & $files[$n])
                            EndIf 
                            
                        
                        EndIf
            EndSelect
        Next
    EndIf

    if IsArray($dirs) then
        for $n = 1 to $dirs[0]
            $retval = _DeleteFileRecursive($sPath & "\" & $dirs[$n], $pattern,$logfile)
            if $retval > 0 then $nFilesFound = $nFilesFound + $retval
        next
    endif
    return $nFilesFound
EndFunc

    

Func _DateDiffInDays($filename)
    if not FileExists($filename) then return -1
    local $filetime =  FileGetTime($filename)
    local $tempDate = $filetime[0] & "/" & $filetime[1] & "/" & $filetime[2]
    local $currDate = @YEAR & "/" & @MON & "/" & @MDAY
    return _DateDiff("D",$tempDate,$currDate)
EndFunc 



Func _DeleteFile($filepath,$age)
    $msg = $msg & "Age: " & $age & "  File: " & $filepath & @CRLF
    FileDelete($filepath)
    
EndFunc



    

Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1]
    If Not FileExists($sPath) Then
        SetError(1)
        Return ""
    EndIf
    If (StringInStr($sFilter, "\")) or (StringInStr($sFilter, "/")) or (StringInStr($sFilter, ":")) or (StringInStr($sFilter, ">")) or (StringInStr($sFilter, "<")) or (StringInStr($sFilter, "|")) or (StringStripWS($sFilter, 8)="") Then
        SetError(2)
    Return ""
    EndIf
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then
        SetError(3)
        Return ""
    EndIf
    $asFileList[0] = 0
    $hSearch = FileFindFirstFile($sPath & "\" & $sFilter)
    If $hSearch=-1 then 
    SetError(0)
    Return ""
    EndIf
    While 1
        $sFile = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If $iFlag = 1 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
        EndIf
        If $iFlag = 2 Then
            If StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
        EndIf
        If $sFile = "." Or $sFile = ".." Then ContinueLoop
        ReDim $asFileList[UBound($asFileList) + 1]
        $asFileList[0] = $asFileList[0] + 1
        $asFileList[UBound($asFileList) - 1] = $sFile
    WEnd
    FileClose($hSearch)
    SetError(0)
    If $asFileList[0] = 0 Then Return ""
    Return $asFileList
EndFunc

And the ini file:

[directory]
Key1="\\IPorNAME\Foldername"
Key2="\\OtherIporNAME\Foldername"

If there are any questions, i´ll answer it.

Greez and thx to all who helped me

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...