Re: (no subject)
Re: (no subject)
- Subject: Re: (no subject)
- From: Nigel Garvey <email@hidden>
- Date: Thu, 25 Oct 2001 00:29:37 +0100
=?iso-8859-1?Q?Daniel_M=F6ller?= wrote on 23 Oct 2001 10:47:53 +0200:
>
I have a question about this script.
>
The script itself seems OK. But when I run it and there for example is 5
>
files in folder "BingoLotto", the script sometimes delete 1 file, and
>
sometimes 4 files. Why??
>
>
I run OS 9.04, and the 5 files in this case are older than 24 hours, so all
>
files should be deleted, right?
>
>
>
tell application "Finder"
>
delete (every file of folder "BingoLotto" of desktop whose creation date
>
is less than (the current date) - 24 * hours)
>
--empty trash
>
end tell
There's nothing actually wrong with the script. In Mac OS 9.0.4 - and in
several other versions - the Finder has a scripting bug whereby the
'whose' expression is unreliable with numeric values such as dates,
sizes, and capacities. I think the bug is fixed in OS 9.1, or there may
be an OSAX solution.
Below is a vanilla solution 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.
on trashOldFiles(theFolder, age)
set theCutoffDate to (current date) - age
tell application "Finder" to set theFiles to (sort theFolder's files
by creation date) as list
set fileCount to (count theFiles)
set {midpoint, oldie1} to {fileCount div 2 + 1, fileCount + 1}
if fileCount > 0 then
if the creation date of (info for item midpoint of theFiles) comes
before theCutoffDate then
repeat with i from 1 to midpoint
if the creation date of (info for item i of theFiles) comes
before theCutoffDate then
set oldie1 to i
exit repeat
end if
end repeat
else
repeat with i from fileCount to midpoint by -1
if the creation date of (info for item i of theFiles) does not
come before theCutoffDate then
set oldie1 to i + 1
exit repeat
end if
end repeat
end if
end if
if oldie1 is greater than fileCount then return {}
tell application "Finder" to delete items oldie1 thru fileCount of
theFiles
end trashOldFiles
trashOldFiles(choose folder, 1 * days)
NG