Re: (no subject)
Re: (no subject)
- Subject: Re: (no subject)
- From: Nigel Garvey <email@hidden>
- Date: Thu, 25 Oct 2001 15:19:26 +0100
Nigel Garvey wrote on Thu, 25 Oct 2001 00:29:37 +0100:
>
Below is a vanilla solution ...
[to the problem of deleting all files over a certain age without using
'whose']
>
... which doesn't involve checking every file in
>
the folder. It uses the 'sort' command (which *does* work) to sort the
>
files by creation date and then works through the result until it finds
>
the border between the files that are older than the cutoff date and
>
those that aren't. It then deletes all the older ones.
>
>
If there are fewer older files than younger files, it's obviously quicker
>
to check them than to check the more numerous younger ones. The opposite
>
is true if there are fewer younger ones than older ones. This script
>
checks which kind is represented at the approximate midpoint of the list
>
and runs the appropriate other loop to that point.
You'd think I'd be at my best at half past midnight. :-\ Having achieved
the concept of checking the middle of the sorted list first, it only
takes one more brain cell to think of doing a binary search for the first
delete-worthy file. This method turns out to be even faster - except when
the proportion of files to be deleted (or not deleted) is very small, in
which case it's very slightly slower.
on deleteOldFiles(theFolder, age)
set cutoffDate to (current date) - age
tell application "Finder" to set fileList to (sort theFolder's files
by creation date) as list
set listLength to (count fileList)
-- The file list is sorted from youngest to oldest.
-- The binary-search right marker will be used to mark the
-- position in the list of the youngest file that qualifies for
deletion.
-- Start by assuming no such file. (Mark it as coming after the list!)
set R to listLength + 1
-- Converge on the first file in the list whose
-- creation date comes before the cutoff date
if listLength > 0 then
set L to 1
repeat until L = R
set M to (L + R) div 2
if the creation date of (info for item M of fileList) comes
before cutoffDate then
set R to M
else
set L to M + 1
end if
end repeat
end if
if R > listLength then return {} -- no qualifying files, or no files
at all
tell application "Finder" to delete items R thru listLength of
fileList
end deleteOldFiles
deleteOldFiles(choose folder, 1 * days)
NG