jprater Posted September 13, 2004 Posted September 13, 2004 I've written a Au3 script to archive logs generated by various devices around my network. The current script is able to rename each file acording to that files' creation date and time, then moves that file into a folder named after the creation date. What I would like to do is have a script that is able to delete folders dated more than 3 months old. I've built a UDF to convert the dates into international format, and I can undo this change to get the date back into a numeric form. Example of current Directory names: 09Sep2004 10Sep2004 11Sep2004 12Sep2004 13Sep2004 I havn't started on the code for this ability yet, and I'm able to come up with an idea that should allow me to delete a folder that is exactly 90 days older. My hang up is that the script may not be run on a daily basis. If I write it so that it deletes the folder exactly 3 months old, it will leave folders that are 91 days or older, and some of the 29-31 day's there. I'm used to the date caclulations inside VB/ASP but can't find anything that refers to this sort of calculation in the help files. If I've been unclear in any way, let me know and I'll clarify my goal. Thanks in advance.
ezzetabi Posted September 13, 2004 Posted September 13, 2004 The easiest way that comes in my mind is convert the filename date in the current day of the year, if the year is not the current one you have to substract 365 (or 366). If that number is lower than @yday - 90 then you can delete it.
Developers Jos Posted September 13, 2004 Developers Posted September 13, 2004 (edited) I havn't started on the code for this ability yet, and I'm able to come up with an idea that should allow me to delete a folder that is exactly 90 days older. My hang up is that the script may not be run on a daily basis. If I write it so that it deletes the folder exactly 3 months old, it will leave folders that are 91 days or older, and some of the 29-31 day's there. I'm used to the date caclulations inside VB/ASP but can't find anything that refers to this sort of calculation in the help files. If I've been unclear in any way, let me know and I'll clarify my goal. Thanks in advance. <{POST_SNAPBACK}>Have a script here i use to delete all files older than 14 days.. Optional also all subdirectories and will delete empty directories. It requiers the datenew.au3 include library which you can find on my Autoit stuf web page ..Just hack it for your use: expandcollapse popup; ; AutoIt Version: 3.0 ; Language: English ; Platform: Win9x/NT ; Author: Jos van der Zande ; ; Script Function: ; Remove file older than 14 days #include <date.au3> AutoItSetOption ( "RunErrorsFatal", 0) ;AutoItSetOption ( "TrayIconHide", 1) Break(0) $Hotkey = "{Pause}" HotKeySet ($Hotkey ,"StopExec") $L_Msg = "" ; init variables $Version = "0.2" $dir="C:\data\user" $Dircount = 0 $n_FileCount = 0 $n_FileSize = 0 $begin = TimerStart() ; _Remove_Files(root dir, inclu subdirs,remove files older than) $rc = _Remove_Files($Dir, 1,14) Exit ;=============================================================================== ; ;=============================================================================== Func _Remove_Files($T_Dir,$T_Incl_SubDir,$T_Days) Dim $n_Dirnames[200000][2]; max number of directories that can be counted Local $n_DirCount = 0 Local $n_File Local $n_Search Local $n_tFile Local $n_Fdate Writelog("### Start run ###") $T_DirCount = 1 $T_FileCount = 0 $T_FileSize = 0 ; remove the end \ If StringRight($T_Dir,1) = "\" then $T_Dir = StringTrimRight($T_Dir,1) $n_Dirnames[$T_DirCount][0] = $T_Dir $n_Dirnames[$T_DirCount][1] = 0 $T_Incl_SubDir = Int($T_Incl_SubDir) ; Exit if base dir doesn't exists If Not FileExists($T_Dir) then Return 0 ; keep on looping until all directories are counted While $T_DirCount > $n_DirCount $n_DirCount = $n_DirCount + 1 $n_Search = FileFindFirstFile($n_Dirnames[$n_DirCount][0] & "\*.*" ) If $n_Search = -1 Then ContinueLoop EndIf ShowMenu("Processing directory:" & @LF & $n_Dirnames[$n_DirCount][0],0) While 1 $n_File = FileFindNextFile($n_Search) If @error Then ExitLoop $n_tFile = $n_Dirnames[$n_DirCount][0] & "\" & $n_File ShowMenu(" File :" & $n_File,2) ; skip these references if $n_File = "." or $n_File = ".." then ContinueLoop EndIf ; if Directory than add to the list to be processed later If StringInstr(FileGetAttrib ( $n_tFile ),"D") > 0 then If $T_Incl_SubDir = 1 then $T_DirCount = $T_DirCount + 1 $n_Dirnames[$T_DirCount][0] = $n_tFile $n_Dirnames[$T_DirCount][1] = 0 EndIf Else ; Check age of the file $n_Fdate = FileGetTime($n_tFile,1) $sdate=$n_Fdate[0] & "/" & $n_Fdate[1] & "/" & $n_Fdate[2] & " " & $n_Fdate[3] & ":" & $n_Fdate[4] & ":" & $n_Fdate[5] $edate=@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC If _DateDiff ('d',$sdate,$eDate) > $T_Days then $rc = FileRecycle($n_tFile) Writelog($sdate & " " & _DateDiff ('d',$sdate,$eDate) & " *** rc:" & $rc & " del-> " & $n_tFile) Else ; add 1 to filecount for this directory Writelog(" Keep -> " & $n_tFile) $n_Dirnames[$n_DirCount][1] = $n_Dirnames[$n_DirCount][1] + 1 EndIf EndIf Wend FileClose($n_Search) Wend Writelog("### Removing empty directories ###") For $x = $T_DirCount to 2 step -1 Writelog("Dir: " & $n_Dirnames[$x][0] & " Files:" & $n_Dirnames[$x][1]) If $n_Dirnames[$x][1] = 0 then If "user\hidden" <> StringRight($n_Dirnames[$x][0],11) then DirRemove ($n_Dirnames[$x][0]) Writelog(" Remove Dir: " & $n_Dirnames[$x][0]) EndIF EndIf Next Writelog("### End Run ###") Return ( 1 ) EndFunc ; ;====================================== ; Stop executing when Hotkey is pressed ;====================================== Func StopExec() $mrc=Msgbox(4,"Cancel Execution?","Your pressed " & $Hotkey & ". Do you want to cancel the Program?") if $mrc = 6 then writelog("*** Utility Cancelled by operator.") Exit EndIf Return EndFunc ;===================================================== ; Show Splash screen or update its content ;===================================================== Func ShowMenu($g_msg,$concat) $W_Title=" file remove Utility (ver:" & $Version & ") Press " & $Hotkey & " to stop execution" $D_msg = "" If $concat > 0 Then If $concat = 1 Then $L_msg = $L_msg & @LF & $g_msg $d_msg = $L_msg Else $d_msg = $L_msg & @LF & $g_msg EndIf Else $L_msg = $g_msg $d_msg = $g_msg EndIf if WinExists($W_Title) then ControlSetText($W_Title,"","Static1",$d_msg) else SplashTextOn($W_Title,$d_msg,800,100,1,1,6,"Courier",10,600) endif Return 0 EndFunc ;============================== ; Write to logfile ;============================== Func WriteLog($msg) $msg = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & "==>" & $msg FileWriteLine(@ScriptDir & "\RemFiles.log", $msg ) Return 0 EndFunc Edited April 11, 2005 by JdeB SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
jprater Posted September 14, 2004 Author Posted September 14, 2004 That looks like it will do what I'm after perfectly. I was hoping there was some sort of function within AutoIt that would allow calander calculations, but that doesn't seem to be the case. Thanks Very much for the help.
sksbir Posted October 6, 2005 Posted October 6, 2005 I revrote the function in order to recursive delete files and subfolders from a folder Don't forget to deact verbose mode (witch writes a log file in TEMP directory) French version (english version follows) expandcollapse popup; ---------------------------------------------------------------------------- ; ; AutoIt Version: 3.1.0 ; ; Menage récursif de tous les fichiers/dossiers plus vieux que NBHEURES heures. ; ; ajout bibliotheque pour _datediff #include <Date.au3> ; ajout bibliotheque pour _FileWriteLog #include <file.au3> Func epur_repertoire($REPERTOIRE,$NBHEURES,$SELFDELETE) Local Const $VERBOSE="OUI" if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Appel epur_repertoire(" & $REPERTOIRE & "," & $NBHEURES & "," & $SELFDELETE & ")" ) ; ; suppression du \ éventuellement trouvé à la fin de l'argument. If StringRight($REPERTOIRE,1) = "\" then $REPERTOIRE = StringTrimRight($REPERTOIRE,1) ; ; validation que l'argument existe et est un répertoire if not(FileExists($REPERTOIRE)) then Return(1) If StringInstr(FileGetAttrib ( $REPERTOIRE ),"D") = 0 then return(2) ; local $SCANREP = FileFindFirstFile($REPERTOIRE & "\*.*") ; Si le répertoire est deja vide... If $SCANREP = -1 Then return(0) ; boucle de traitement de tout le contenu While 1 local $FICHIER = FileFindNextFile($SCANREP) If @error Then ExitLoop if $FICHIER = "." or $FICHIER = ".." then ContinueLoop ; Appel recursif pour tous les sous-répertoires, utilisation de l'option selfdelete pour nettoyer l'arboresence de ses répertoires vides. If StringInstr(FileGetAttrib ( $REPERTOIRE & "\" & $FICHIER ),"D") = 1 then epur_repertoire($REPERTOIRE & "\" & $FICHIER,$NBHEURES,1) Else ; recherche sur la date de modification. local $DATEFIC= FileGetTime($REPERTOIRE & "\" & $FICHIER,0,0) local $DIFF1=_DateDiff("h", $DATEFIC[0] & "/" & $DATEFIC[1] & "/" & $DATEFIC[2] & " " & $DATEFIC[3] & ":" & $DATEFIC[4] & ":" & $DATEFIC[5], _NowCalc()) ; recherche sur la date de création ( vieux fichier récemment déplacés par exemple ) local $DATEFIC= FileGetTime($REPERTOIRE & "\" & $FICHIER,1,0) local $DIFF2=_DateDiff("h", $DATEFIC[0] & "/" & $DATEFIC[1] & "/" & $DATEFIC[2] & " " & $DATEFIC[3] & ":" & $DATEFIC[4] & ":" & $DATEFIC[5], _NowCalc()) ; suppression si les 2 dates sont antérieures à la limite. if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Fichier=" & $FICHIER & ",DIFF1=" & $DIFF1 & ",DIFF2=" & $DIFF2) if ($DIFF1 > $NBHEURES) and ($DIFF2 > $NBHEURES) then local $RETCOD=FileDelete ($REPERTOIRE & "\" & $FICHIER ) if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Suppression Fichier " & $FICHIER & ", RETCOD=" & $RETCOD ) EndIf EndIf WEnd ; vital : fermer le scan du rep avant de tenter la suppression du répertoire scanné. FileClose($SCANREP) ; on s'occupe du répertoire courant. if $SELFDELETE = 1 Then ; pas de suppression récursive (of course!!) local $RETCOD=DirRemove ($REPERTOIRE,0 ) if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Suppression repertoire=" & $REPERTOIRE & ", RETCOD=" & $RETCOD ) EndIf EndFunc ; ; exemple d'appel: ; $CHOIXREP=FileSelectFolder ( "Choisissez le répertoire à épurer : ", "" , -1 , @TempDir ) $RETENTION=InputBox("Rétention","Tapez la rétention en heure:",48," M") epur_repertoire( $CHOIXREP,$RETENTION,0) ; Same version, with comments translated in english ( replace variables names at will ) expandcollapse popup; ---------------------------------------------------------------------------- ; ; AutoIt Version: 3.1.0 ; ; recursive delete for all files and subfolders older than NBHEURES hours. ; ; include for _datediff #include <Date.au3> ; include for _FileWriteLog #include <file.au3> Func epur_repertoire($REPERTOIRE,$NBHEURES,$SELFDELETE) Local Const $VERBOSE="OUI" if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Appel epur_repertoire(" & $REPERTOIRE & "," & $NBHEURES & "," & $SELFDELETE & ")" ) ; ; remove the "\" at end of given path. If StringRight($REPERTOIRE,1) = "\" then $REPERTOIRE = StringTrimRight($REPERTOIRE,1) ; ; check that given path exists and is a directory if not(FileExists($REPERTOIRE)) then Return(1) If StringInstr(FileGetAttrib ( $REPERTOIRE ),"D") = 0 then return(2) ; local $SCANREP = FileFindFirstFile($REPERTOIRE & "\*.*") ; if directory is already empty... If $SCANREP = -1 Then return(0) ; loop for each item in given folder While 1 local $FICHIER = FileFindNextFile($SCANREP) If @error Then ExitLoop if $FICHIER = "." or $FICHIER = ".." then ContinueLoop ; recursive call for each subdirectory, activating "selfdelete" option in order to remove empty directories If StringInstr(FileGetAttrib ( $REPERTOIRE & "\" & $FICHIER ),"D") = 1 then epur_repertoire($REPERTOIRE & "\" & $FICHIER,$NBHEURES,1) Else ; seeking for modification date local $DATEFIC= FileGetTime($REPERTOIRE & "\" & $FICHIER,0,0) local $DIFF1=_DateDiff("h", $DATEFIC[0] & "/" & $DATEFIC[1] & "/" & $DATEFIC[2] & " " & $DATEFIC[3] & ":" & $DATEFIC[4] & ":" & $DATEFIC[5], _NowCalc()) ; seeking for creation date ( example : old files recently moved here) local $DATEFIC= FileGetTime($REPERTOIRE & "\" & $FICHIER,1,0) local $DIFF2=_DateDiff("h", $DATEFIC[0] & "/" & $DATEFIC[1] & "/" & $DATEFIC[2] & " " & $DATEFIC[3] & ":" & $DATEFIC[4] & ":" & $DATEFIC[5], _NowCalc()) ; deleting if condition matching if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Fichier=" & $FICHIER & ",DIFF1=" & $DIFF1 & ",DIFF2=" & $DIFF2) if ($DIFF1 > $NBHEURES) and ($DIFF2 > $NBHEURES) then local $RETCOD=FileDelete ($REPERTOIRE & "\" & $FICHIER ) if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Suppression Fichier " & $FICHIER & ", RETCOD=" & $RETCOD ) EndIf EndIf WEnd ; mandatory : close current directory scan before trying to suppress it. FileClose($SCANREP) ; try to suppress current dir. if $SELFDELETE = 1 Then ; NOT récursive (of course!!) local $RETCOD=DirRemove ($REPERTOIRE,0 ) if $VERBOSE="OUI" then _FileWriteLog(@TempDir & "\test_epur.log","Suppression repertoire=" & $REPERTOIRE & ", RETCOD=" & $RETCOD ) EndIf EndFunc ; ; example: ; $CHOIXREP=FileSelectFolder ( "Choose directory to clean : ", "" , -1 , @TempDir ) $RETENTION=InputBox("hourlimit","give hour limit:",48," M") epur_repertoire( $CHOIXREP,$RETENTION,0) ;
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