Sign in to follow this  
Followers 0
tomster82

Delete Files depending on Date

8 posts in this topic

Hi @ all!

I´m new @ this forum and a newbee @ working with auto it.

I have to write a tool which checks the date of files in folders and their subfolders. If the files are older than half a year they have to be deleted.

In a folder there should not be more than 20 files, and if the file is younger than 4 weeks it shouldn´t be deleted.

is there anybody who has an idea how to to this?

Don´t think that i´m to leasy to do by myself but it a little bit difficult because i´m an noob.

I would be very gratefull i´ve u could help me

thx a lot

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Welcome to the forums!

You can use the FileFindFirstFile() and FileFindNextFile() functions to get your list of files, and then you can issue the FileGetTime() function on each file found to achieve your goal.

While performing the FileFind...() actions you can keep a count of each file that you find, so that you know how many files exist in the folder. You can then determine how many files you need to delete (after perhaps comparing with the number of files older than four weeks) in order to achieve your goal.

P.S. Future support questions should go into the Support forum. ;)

Edited by LxP

Share this post


Link to post
Share on other sites

Thank u very much for welcome me.

And thx much more for the help.

I´m so sorry that i´ve chosen the wrong kind of thread. :P Next time I´ll write into the support forum. ;)

Thx & Greez

Tomster

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Edited by tomster82
1 person likes this

Share this post


Link to post
Share on other sites

Here a short summary of the funcionality of this programm. ;)

1. The Programm checks the date of found files.

2. The Program calculate the difference between the actual date and a userdefined date.

3.There are different cases defined wich dissallow the program to check date or to delete.

4.There are different file names defined wich dissallow the program to check date or to delete.

5. In the "else case" the program deletes the file and writes it´s age and name in a log file.

6. In the ini file u can define @ which location the programs should check. It´s possible to check @ different servers

There might be some functions that are not listed here. :P

I´m so sorry

Share this post


Link to post
Share on other sites

Here a short summary of the funcionality of this programm. ;)

1. The Programm checks the date of found files.

2. The Program calculate the difference between the actual date and a userdefined date.

3.There are different cases defined wich dissallow the program to check date or to delete.

4.There are different file names defined wich dissallow the program to check date or to delete.

5. In the "else case" the program deletes the file and writes it´s age and name in a log file.

6. In the ini file u can define @ which location the programs should check. It´s possible to check @ different servers

There might be some functions that are not listed here. :P

I´m so sorry

I get an error when running your script, see below:

C:\Download\!Project\PrgLang\AutoIt-v3\Examples\DirDateDel.au3(135,58) : ERROR: _FileListToArray() already defined.

Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

Nice idea, now for some comments.. :oops:

One, just to add more complexity to your life and simplicity for others you might want to create a window that allows you to browse for directories that you want to delete v.s. having to have them in an {.INI} file or am I mistaken? This will take a GUI.

Two, Also, I notice that you hard coded directories <lines 74-80> you want to disallow checking.. You might want to add these to your {.INI} file under a different tag call [PROTECTED] or [DISALLOW].

Three, recursive deleting can be dangerous when dealing with the C:\*.* scenario. There should be situations that cannot happen.

For instance: (example: Folder\FIles to [PROTECT])

C:\*.*

C:\Windows\*.*

C:\Winnt\*.*

C:\Documents & Settings\*.*

I like your idea. With a front-end on this it will become a really nice script.. Keep it going!

Cheer.. :mad2:

note: Tested using Beta 3.1.1.75

Share this post


Link to post
Share on other sites

There are the easy to custom .ini entries .....

If there is time i´ll try to make a GUI... never done before--- could last a second ;-)

I´ve tried the scipt i´ve posted again, there is no error like u reported.

Did u copy n paste?

Share this post


Link to post
Share on other sites

There are the easy to custom .ini entries .....

If there is time i´ll try to make a GUI... never done before--- could last a second ;-)

I´ve tried the scipt i´ve posted again, there is no error like u reported.

Did u copy n paste?

If you have not created a GUI before there are a few awesome examples of scripts out there. Check out this thread: Klip (clipboard buffer program with a wonderful GUI)

http://www.autoitscript.com/forum/index.ph...opic=15760&st=0

Yes, I did cut & paste. Not sure why the error occured. I will try it again from you last post. I will let you know the results..

Cheers.. ;)

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