Re: newest file in a folder
Re: newest file in a folder
- Subject: Re: newest file in a folder
- From: Paul Berkowitz <email@hidden>
- Date: Sun, 17 Dec 2000 16:11:44 -0800
On 12/17/00 3:31 PM, "g3pb" <email@hidden> wrote:
>
> What is the clean and easy way to get an alias to the newest file of
>
> a folder? (the one with the newest modification date.)
>
-- this should do it, note you will need to change the destination folder
>
>
tell application "Finder"
>
set numItems to count of items in alias "iMac:Desktop Folder:AMA:"
>
if numItems is not 1 then
>
set aList to every item in alias "iMac:Desktop Folder:AMA:" as alias
>
list
>
else
>
set a to {every item in alias "iMac:Desktop Folder:AMA:" as alias}
>
end if
>
set latestFile to modification date of item 1 in aList
>
repeat with anItem in aList
>
if modification date of anItem > latestFile then
>
set latestFile to modification date of anItem
>
end if
>
end repeat
>
end tell
>
Well, that's one way, but it's certainly not the cleanest and easiest. It
might take the Finder forever, if there are a huge number of items in the
folder, and it might even poop out "Apple Event timed out". The fastest and
most reliable way is probably to use Akua Sweets scripting addition. Using
the same technique as g3pb:
set folderPath to "Hard Disk:Some Folder:Your Folder:"
set theFiles to the entries in alias folderPath whose kinds are a file
that were modified after date "12/1/00" as alias -- or another recent cutoff
date
try
set latestFile to item 1 of theFiles
on error
display dialog "Pick an earlier date." with icon 2
return
end try
set latestDate to modification date of (get basic info for latestFile)
repeat with i from 1 to (count theFiles)
set theFile to item i of theFiles
set itsDate to modification date of (get basic info for theFile)
if itsDate > latestDate then
set latestDate to itsDate
set latestFile to theFile
end if
end repeat
latestFile
There's another way without a repeat loop using Akua's 'order list', but you
have to make lists to link the the date to the file. By picking a good
cut-off date above, the list will always be small enough that this should be
fastest and easiest. If you pick too recent a date, you get told
immediately.
--
Paul Berkowitz