tomster82 Posted September 13, 2005 Share Posted September 13, 2005 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 More sharing options...
MrSpacely Posted September 13, 2005 Share Posted September 13, 2005 (edited) I looked in the helpfile but could not find a real function for this. Maybe this is usefull expandcollapse popup#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 September 13, 2005 by MrSpacely Link to comment Share on other sites More sharing options...
/dev/null Posted September 13, 2005 Share Posted September 13, 2005 This is for understanding, my Problem is how to compare or calculate with dates???Check out _DateDiff().CheersKurt __________________________________________________________(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 More sharing options...
MrSpacely Posted September 13, 2005 Share Posted September 13, 2005 (edited) Check out _DateDiff().CheersKurt<{POST_SNAPBACK}>Won't work;)datediff accepts dates with / in itFilegetdate returns date without / or an arrayYou need to make almost same work arround to add those / charsSame 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 Edited September 13, 2005 by MrSpacely Link to comment Share on other sites More sharing options...
tomster82 Posted September 13, 2005 Author Share Posted September 13, 2005 (edited) _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 September 13, 2005 by tomster82 Link to comment Share on other sites More sharing options...
/dev/null Posted September 13, 2005 Share Posted September 13, 2005 Won't work;)datediff accepts dates with / in itWon't work? Sure it will ... You need to make almost same work arround to add those / charsSame 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 Well, let's see if we can make an even smarter function #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) endfuncO.K., mine is 11 inches, erm... lines long. How long is yours? CheersKurt __________________________________________________________(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 More sharing options...
MrSpacely Posted September 13, 2005 Share Posted September 13, 2005 (edited) shorter is better tomster did not even mention mine that not nice I am beaten its useless to resist but for looking thru folders try /dev/null I am tired Edited September 13, 2005 by MrSpacely Link to comment Share on other sites More sharing options...
tomster82 Posted September 13, 2005 Author Share Posted September 13, 2005 i mentioned it but i ve found this solution by myself. When i´m @ home i would check out both ways off u. Thank u for the help Link to comment Share on other sites More sharing options...
/dev/null Posted September 13, 2005 Share Posted September 13, 2005 shorter is better Yes, size does not matter. At least that's what they tell us... tomster did not even mention mine that not nice Ooooohhhhh, come on, your code was equally good!! Really !! No need to be sad!! but for looking thru folders try /dev/null I am tiredLet's help him together. Any idea?CheersKurt __________________________________________________________(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 More sharing options...
seandisanti Posted September 13, 2005 Share Posted September 13, 2005 Let's help him together. Any idea?CheersKurt<{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 More sharing options...
/dev/null Posted September 13, 2005 Share Posted September 13, 2005 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 CheersKurt __________________________________________________________(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 More sharing options...
MrSpacely Posted September 13, 2005 Share Posted September 13, 2005 I still say smaller is better why have 10 inch if you can walk easy with ow wait wrong forum Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted September 13, 2005 Moderators Share Posted September 13, 2005 I still say smaller is better 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? 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 More sharing options...
/dev/null Posted September 13, 2005 Share Posted September 13, 2005 (edited) Have we been reduced to talking about the genetic make up of an autoit script? <{POST_SNAPBACK}>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).[sCRIPTPOST: Recursive File Delete]expandcollapse popup#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" .... ; endfuncCheersKurt Edited December 5, 2006 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 More sharing options...
seandisanti Posted September 13, 2005 Share Posted September 13, 2005 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).expandcollapse popup#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" .... ; endfuncCheersKurt<{POST_SNAPBACK}>what no log of what was deleted? pfft Link to comment Share on other sites More sharing options...
/dev/null Posted September 13, 2005 Share Posted September 13, 2005 (edited) what no log of what was deleted? pfft<{POST_SNAPBACK}>pfft to yourself ... , but actually you're right. Here we go...expandcollapse popup#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 !! ; endfuncCheersKurt Edited September 13, 2005 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 More sharing options...
tomster82 Posted September 14, 2005 Author Share Posted September 14, 2005 (edited) 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 September 14, 2005 by tomster82 Link to comment Share on other sites More sharing options...
seandisanti Posted September 14, 2005 Share Posted September 14, 2005 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 More sharing options...
tomster82 Posted September 14, 2005 Author Share Posted September 14, 2005 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? Link to comment Share on other sites More sharing options...
seandisanti Posted September 14, 2005 Share Posted September 14, 2005 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now