homunculus Posted May 25, 2007 Posted May 25, 2007 Hi, as I can't post in the UDF section I'll just do it here. I've created a UDF which deletes old files with a certain naming convention, such as logfiles with a certain date format.For example nnCron lite logs with the following format: 0525cron.logor Spybot logs with the following format: Checks.070521-0900.logThis UDF takes the following options:Working directoryFile name patternNumber of files to keepTo delete all except the latest 14 nnCron Lite logs, do this:#include <OldFileDeleteFunction.au3>OldFileDeleteFunction('C:\Program Files\cron\log','????cron.log',14)To delete all except the latest 10 SpyBot S&D logs, do this:#include <OldFileDeleteFunction.au3>OldFileDeleteFunction('C:\Documents and Settings\All Users\Application Data\Spybot - Search & Destroy\Logs',Checks.??????-????.log',10)And so on. Please let me know what you think.Func OldFileDeleteFunction($Directory,$FileSpec,$KeepFiles) If FileChangeDir($Directory) = 0 Then Exit $OldFileFinder = FileFindFirstFile($FileSpec) If $OldFileFinder = -1 Then Exit $OldFileArray = '' While 1 $OldFile = FileFindNextFile($OldFileFinder) If @error Then ExitLoop $OldFileArray &= @CRLF & $OldFile WEnd FileClose($OldFileFinder) $SortProcess = Run('sort.exe /r ', @SystemDir, @SW_HIDE, 1 + 2) ; $STDIN_CHILD + $STDOUT_CHILD, provided as numbers so we don't have to include the Constants file. StdinWrite($SortProcess, $OldFileArray) StdinWrite($SortProcess) $String = StdoutRead($SortProcess) $FileArray = StringSplit(StringStripCR($String), @LF) If $FileArray[0] > $KeepFiles Then For $e = $KeepFiles + 1 To $FileArray[0] - 1 If FileExists($FileArray[$e]) Then FileDelete($FileArray[$e]) Next EndIf EndFunc
homunculus Posted May 25, 2007 Author Posted May 25, 2007 @ homunculus: Hi, and welcome. I think it's indeed a useful UDF, thanks for sharing. Let me make some comments: - I think you meant RETURN, not EXIT - You change the working dir. May be it worth to restore it at the end, or modify the function to not change it - Win98SE (at least my version) do not have sort.exe - You could use _FileListToArray to create the file array. It is not documented, but I always have received the array already sorted. Anyway, you have _ArraySort, no need for sort.exe. - You may use the check "If $FileArray[0] > $KeepFiles Then..." near the end of UDF for clarity, but it is not strictly necessary: the For loop will take care about that (it is AutoIt, man!) Have a good time alc Hi Alc, Thanks for your comments, much appreciated. - I used Exit because Exit really means Exit. However, Return is probably better. - Modifying the function not to change the working dir is a good idea. - I made (and use) the script for Windows XP - I wasn't aware this function already exists and was actually a waste of time... - Indeed, I didn't need the For-Next loop. So, this UDF can be moved into the trashcan.
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