advancix Posted June 4, 2010 Share Posted June 4, 2010 hi, I`m totally new to AutoIt and scripting in general. I need to clean up a directory, where a backup-application saves all full & differential-backups. I need to know how is it possible to delete all diff-backup-files except the 2 latest ones. I already managed to modify another script for my needs, so that it removes files with specific extension older than x days. AutoIt & it`s community seems to be great, but has way too much functions for a noob like me. thanks in advance, advancix PS: sorry for my bad english. Link to comment Share on other sites More sharing options...
Zibit Posted June 4, 2010 Share Posted June 4, 2010 (edited) $t = FileGetTime(@ScriptDir & "\yourfile.exe") if $t > "your time" then filedelete(@ScriptDir & "\yourfile.exe") this is a simple example... use _FileListToArray in a loop to use this... for ex: $array = _FileListToArray(@ScriptDir & "\backup", "*", 1) $i = $i + 1 do $time = FileGetTime(@ScriptDir & "\backup" & $array[$i]) if $time > "your time" then filedelete(@ScriptDir & "\" & $array[$i]) until $array[0] = $i Edited June 4, 2010 by Zibit Creator Of Xtreme DevelopersPixel Pattern UDFTray GUI UDFMathssend & recive register scriptMouse Control via Webcam Link to comment Share on other sites More sharing options...
advancix Posted June 7, 2010 Author Share Posted June 7, 2010 thx a lot for the script, i had to modify it a bit, but it doesn't work. #include <Date.au3> #Include <File.au3> Dim $i $sFolder = InputBox("Backup-Cleaner", "Please type in filepath correctly", "C:\Downloads") If @error = 1 Then MsgBox(4096, "Backup-Cleaner", "Aborted by User") Exit EndIf $iMaxDays = InputBox("Backup-Cleaner", "Type in the age of files which should be removed e.g.= 14", "14") If @error = 1 Then MsgBox(4096, "Backup-Cleaner", "Aborted by User") Exit EndIf $t = FileGetTime($sFolder & "\*.doc") if $t > $iMaxDays Then filedelete($sFolder & "\*.doc") $array = _FileListToArray($sFolder , "*", 1) $i = $i + 1 do $time = FileGetTime($sFolder & $array[$i]) if $time > $iMaxDays Then filedelete($sFolder & "\" & $array[$i]) until $array[0] = $i This is the modified script, but somehow it doesn`t work. Seems to stuck in a loop, maybe it has sth. to do with "$i". Link to comment Share on other sites More sharing options...
Zibit Posted June 7, 2010 Share Posted June 7, 2010 $i = $i + 1 must be in a loop not outside it... like this: #include <Date.au3> #Include <File.au3> Dim $i $sFolder = InputBox("Backup-Cleaner", "Please type in filepath correctly", "C:\Downloads") If @error = 1 Then MsgBox(4096, "Backup-Cleaner", "Aborted by User") Exit EndIf $iMaxDays = InputBox("Backup-Cleaner", "Type in the age of files which should be removed e.g.= 14", "14") If @error = 1 Then MsgBox(4096, "Backup-Cleaner", "Aborted by User") Exit EndIf $t = FileGetTime($sFolder & "\*.doc") if $t > $iMaxDays Then filedelete($sFolder & "\*.doc") $array = _FileListToArray($sFolder , "*", 1) do $i = $i + 1 $time = FileGetTime($sFolder & $array[$i]) if $time > $iMaxDays Then filedelete($sFolder & "\" & $array[$i]) until $array[0] = $i Creator Of Xtreme DevelopersPixel Pattern UDFTray GUI UDFMathssend & recive register scriptMouse Control via Webcam Link to comment Share on other sites More sharing options...
99ojo Posted June 7, 2010 Share Posted June 7, 2010 (edited) Hi, 1) FileGetTime doesn't work with Wildcards. 2) FileGetTime returns an array. Try this: #include <Date.au3> #Include <File.au3> Dim $i, $arfile $sFolder = InputBox("Backup-Cleaner", "Please type in filepath correctly", "C:\Downloads") If @error = 1 Then MsgBox(4096, "Backup-Cleaner", "Aborted by User") Exit EndIf $iMaxDays = InputBox("Backup-Cleaner", "Type in the age of files which should be removed e.g.= 14", "14") If @error = 1 Then MsgBox(4096, "Backup-Cleaner", "Aborted by User") Exit EndIf ;get all files into array $arfile = _FileListToArray ($sFolder, "*.doc", 1) If @error Then MsgBox (0,"Error", "Path or no documents found!!") Exit EndIf For $i = 1 To UBound ($arfile) - 1 ;get modified date of file, return is an array $t = FileGetTime ($sFolder & "\" & $arfile [$i]) ;if difference is bigger then input in days -> delete file, see helpfile _DateDiff () If _DateDiff ("D", $t [0] & "/" & $t [1] & "/" & $t [2], _NowCalcDate ()) > $iMaxDays Then FileDelete ($sFolder & "\" & $arfile [$i]) EndIf Next ;-)) Stefan Edited June 7, 2010 by 99ojo Link to comment Share on other sites More sharing options...
advancix Posted June 7, 2010 Author Share Posted June 7, 2010 @99ojo: now it works, thx a lot, vielen Dank. greez advancix 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