Jump to content

Delete Files depending on their mod time


tomster82
 Share

Recommended Posts

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.

and if the file is younger than 4 weeks it shouldn´t be deleted.

This is for understanding, my Problem is how to compare or calculate with dates???

My Program could get the date of the files but i don´t know how i could realise it, that the programm don´t delete a file with the date of 31.08.2005 when we have the 01.09.2005. I´ve tried much ways but in no case the right. ;)

Could anybody help a noob like me? I would be very thankful.

Greez to all autoIt users

Link to comment
Share on other sites

I looked in the helpfile but could not find a real function for this.

Maybe this is usefull

#include <date.au3>
;Example usage

msgbox(0,"","Mountain.bmp is " & FileDaysModified(@windowsdir & "\\notepad.exe") & " days old")

;-----------Functions in part -------

Func DateArraytoDayValue($filename)
  if FileExists($filename) then
    $filedate = FileGetTime ( $filename,0,0)
    return _DateToDayValue ( $filedate[0], $filedate[1],$filedate[2])
  else 
    return -1
  endif
EndFunc

Func CurrentDayValue()
  $datearray  = StringSplit (_NowCalcDate(), "/")
  return _DateToDayValue($datearray[1],$datearray[2],$datearray[3])
EndFunc

;-------End Functions in part -------


;========Complete function inputs a filename with path return how many days old only 
;========works for files younger then since noon 4713 BC January 1 

Func FileDaysModified($filename)
  if FileExists($filename) then
    $datearray  = StringSplit (_NowCalcDate(), "/")
    $filedate = FileGetTime ( $filename,0,0)
    $currentdays = _DateToDayValue($datearray[1],$datearray[2],$datearray[3])   
    $filedays = _DateToDayValue ( $filedate[0], $filedate[1],$filedate[2])
    return $currentdays - $filedays
  else 
    return -1
  endif
EndFunc

To compare simply use 14 days for 2 weeks and 183 days for half a year

also its useless to make function wich doesn't delete files younger then 14 days because it only deletes files older then half a year.

So that function would be called for files younger then 14 days and just do nothing bit useless

Edited by MrSpacely
Link to comment
Share on other sites

This is for understanding, my Problem is how to compare or calculate with dates???

Check out _DateDiff().

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Check out _DateDiff().

Cheers

Kurt

<{POST_SNAPBACK}>

Won't work;)datediff accepts dates with / in it

Filegetdate returns date without / or an array

You need to make almost same work arround to add those / chars

Same amount of work only mine is completed;)

Just use the last function works fine and no need to add / chars

wich would be much work stringmid or something few times far worse;)

And better because I am really smart man ;):P

Edited by MrSpacely
Link to comment
Share on other sites

_DateDiff works fine. i didi it like this:

TimeDiff = _DateDiff('D', $t[0] & "/" & $t[1] & "/" & $t[2], @YEAR & "/" & @MON & "/" & @MDAY,)

the array i got from $t = FileGetTime

it´s very nice off u to help me!!! Thank u very much for the code ;)

so far

But now there is another problem, the program has to check several folders.

If i use the absolut path it works but how to tell my beginner prog to jump trough the folders? There are even some folders which shouldn´t be checked...

Do u have an idea?

When my question are too much tell me please.

Greez Tomster

Edited by tomster82
Link to comment
Share on other sites

Won't work;)datediff accepts dates with / in it

Won't work? Sure it will ... ;)

You need to make almost same work arround to add those / chars

Same amount of work only mine is completed;)

Just use the last function works fine and no need to add / chars

wich would be much work stringmid or something few times far worse;)

And better because I am really smart man :dance:  :dance:

Well, let's see if we can make an even smarter function :P

#include <Date.au3>

msgbox(0,"INFO","notepad.exe is " & _DateDiffInDays(@windowsdir & "\\notepad.exe") & " days old")

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

O.K., mine is 11 inches, erm... lines long. How long is yours? :mad2::oops:

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

shorter is better :mad2:

Yes, size does not matter. At least that's what they tell us... :P

tomster did not even mention mine that not nice  ;)

Ooooohhhhh, come on, your code was equally good!! Really !! No need to be sad!! :oops:

but for looking thru folders try /dev/null I am tired

Let's help him together. Any idea?

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Let's help him together. Any idea?

Cheers

Kurt

<{POST_SNAPBACK}>

if i'm not mistaken, there are atleast 2 recursive file search functions on the forum somewhere....in the support forum alone. and i wouldn't be surprised if there were a few in scripts and scraps too...

and not giving that as a "why didn't you look before asking" but as an "i'd look myself to provide a link but i'm waaaaaay too lazy"

Link to comment
Share on other sites

if i'm not mistaken, there are atleast 2 recursive file search functions on the forum somewhere....in the support forum alone.  and i wouldn't be surprised if there were a few in scripts and scraps too...

You mean like this one? http://www.autoitscript.com/forum/index.ph...488entry71488 ;)

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Moderators

:mad2:  ;)  :P  :dance:  :dance:

I still say smaller is better :cheer: why have 10 inch if you can walk easy with ow wait wrong forum

<{POST_SNAPBACK}>

Have we been reduced to talking about the genetic make up of an autoit script? :oops::cheer:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Have we been reduced to talking about the genetic make up of an autoit script? ;):P

<{POST_SNAPBACK}>

Yeah, let's get back to business. :mad2:

Although there are some recursive file delete functions in the forums, I was in the mood to write YARFDF (yet another recursive file delete function).

[sCRIPTPOST: Recursive File Delete]

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

const $MAXAGE = 100 ; days
global $msg = ""

$retval = _FileDeleteRecursive("C:\temp","*.*",$MAXAGE)
msgbox(0,"","Number of Files found: " & $retval)
ClipPut($msg)

func _FileDeleteRecursive($sPath, $pattern, $maxage)
     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]
            $age = _DateDiffInDays($sPath & "\" & $files[$n])
            if $age > $maxage then 
               _DeleteFile($sPath & "\" & $files[$n],$age)
               $nFilesFound += 1
            endif
        next
     endif

     if IsArray($dirs) then
        for $n = 1 to $dirs[0]
             $retval = _FileDeleteRecursive($sPath & "\" & $dirs[$n], $pattern,$maxage)
           if $retval > 0 then $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
;
; Change this function to actually delete the file !! Be careful !! It's recursive !!
; Wrong path and pattern, and your data is "gone" ....
;
endfunc

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Yeah, let's get back to business.  ;)

Although there are some recursive file delete functions in the forums, I was in the mood to write YARFDF (yet another recursive file delete function).

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

const $MAXAGE = 100    ; days
global $msg = ""

$retval = _FileDeleteRecursive("C:\temp","*.*",$MAXAGE)
msgbox(0,"","Number of Files found: " & $retval)
ClipPut($msg)

func _FileDeleteRecursive($sPath, $pattern, $maxage)
     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]
            $age = _DateDiffInDays($sPath & "\" & $files[$n])
            if $age > $maxage then 
               _DeleteFile($sPath & "\" & $files[$n],$age)
               $nFilesFound += 1
            endif
        next
     endif

     if IsArray($dirs) then
        for $n = 1 to $dirs[0]
             $retval = _FileDeleteRecursive($sPath & "\" & $dirs[$n], $pattern,$maxage)
           if $retval > 0 then $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
;
; Change this function to actually delete the file !! Be careful !! It's recursive !!
; Wrong path and pattern, and your data is "gone" ....
;
endfunc

Cheers

Kurt

<{POST_SNAPBACK}>

what no log of what was deleted? pfft
Link to comment
Share on other sites

what no log of what was deleted?  pfft

<{POST_SNAPBACK}>

pfft to yourself ... ;):P , but actually you're right. Here we go...

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

const $LOGFILE = "C:\temp\deleted-for-the-sake-of-cameronsdad.log"; <=== LOOK HERE !!!;-))
const $MAXAGE = 100    ; days
global $msg = ""

$retval = _DeleteFileRecursive("C:\temp","*.*",$MAXAGE,$LOGFILE)
msgbox(0,"","Number of Files found: " & $retval)
ClipPut($msg)

func _DeleteFileRecursive($sPath, $pattern, $maxage,$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]
            $age = _DateDiffInDays($sPath & "\" & $files[$n])
            if $age > $maxage then 
               $nFilesFound += 1
               _DeleteFile($sPath & "\" & $files[$n],$age)
               if $logfile <> "" then
                  _FileWriteLog($logfile, "Deleted " & $age & " days old file: " & $sPath & "\" & $files[$n])
               endif 
            endif
        next
     endif

     if IsArray($dirs) then
        for $n = 1 to $dirs[0]
             $retval = _DeleteFileRecursive($sPath & "\" & $dirs[$n], $pattern,$maxage,$logfile)
           if $retval > 0 then $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
   ;
   ; Change this function to actually delete the files !!
   ;
endfunc

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

You are both the best, you don´t now how much u helped me.

I thank u sooooo much.

i`ve added the function that u can choose which folders are checked and which not. And in some folders the files are allowed to be older than in others.

All is easy to customise at the begining of the script.

What do u think should i post the whole script or doesen´t need anybody a script like this?

Edited by tomster82
Link to comment
Share on other sites

You are both the best, you don´t now how much u helped me.

I thank u sooooo much.

i`ve added the function that u can choose which folders are checked and which not. And in some folders the files are allowed to be older than in others.

All is easy to customise at the begining of the script.

What do u think should i post the whole script or doesen´t need anybody a script like this?

<{POST_SNAPBACK}>

yeah, definitely post final version...
Link to comment
Share on other sites

I promise that when it´s finally commented i will post it. Will be tomorrow!!!

Should i post it here in support or in the scripts n scrabs part?

<{POST_SNAPBACK}>

could post in scripts and scraps if you want, but i think you should have the finished product on this post too, just so those reading along but not posting see the final result.
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...