Re: Finding the oldest date in a list
Re: Finding the oldest date in a list
- Subject: Re: Finding the oldest date in a list
- From: Graff <email@hidden>
- Date: Tue, 18 May 2004 18:24:23 -0400
This should do under AppleScript 1.9.0 or later:
----
set BackUpFolder to {"5/11/2004", "5/12/2004", "5/13/2004",
"5/14/2004", "5/17/2004", "5/18/2004"}
set oldestDate to date (item 1 of BackUpFolder)
set dateList to rest of BackUpFolder
repeat with textDate in dateList
if (date textDate) < oldestDate then
set oldestDate to date textDate
end if
end repeat
set theName to "" & (month of oldestDate as number) & "/" & day of
oldestDate & "/" & year of oldestDate
----
For AppleScript prior to 1.9.0 you need this script:
----
set BackUpFolder to {"5/11/2004", "5/12/2004", "5/13/2004",
"5/14/2004", "5/17/2004", "5/18/2004"}
set oldestDate to date (item 1 of BackUpFolder)
set dateList to rest of BackUpFolder
repeat with textDate in dateList
if (date textDate) < oldestDate then
set oldestDate to date textDate
end if
end repeat
set theName to "" & (MonthToNum(oldestDate) & "/" & day of oldestDate &
"/" & year of oldestDate
on MonthToNum(theDate)
if class of theDate is date then
set monthList to {"January", "Febuary", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December"}
repeat with i from 1 to 12
if ((month of theDate as string) = (item i of monthList)) then
return i
end if
end repeat
end if
return 0
end MonthToNum
----
You could also do this through straight string manipulation:
----
set BackUpFolder to {"5/11/2004", "5/12/2004", "5/13/2004",
"5/14/2004", "5/17/2004", "5/18/2004"}
set oldestFile to item 1 of BackUpFolder
repeat with theFile in (rest of BackUpFolder)
if ConvertDate(theFile) < ConvertDate(oldestFile) then
set oldestFile to theFile
end if
end repeat
on ConvertDate(theText)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set delimList to text items of theText
set numString to item 3 of delimList
if length of item 2 of delimList is 1 then
set numString to numString & "0"
end if
set numString to numString & item 2 of delimList
if length of item 1 of delimList is 1 then
set numString to numString & "0"
end if
set numString to numString & item 1 of delimList
--numString
set AppleScript's text item delimiters to oldDelims
return numString
end ConvertDate
- Ken
On May 18, 2004, at 4:14 PM, Steven Valenti wrote:
I would like to find the oldest date in a list but just can't seem to
think how to evaluate all of the items. Can anyone help? The items in
this list are names of folders that were created in a backup script and
after 5 days I need to delete the oldest backup.
set BackUpFolder to {"5/11/2004", "5/12/2004", "5/13/2004",
"5/14/2004", "5/17/2004", "5/18/2004"}
set FirstDate to item 1 of BackUpFolder
set SecondDate to item 2 of BackUpFolder
date FirstDate < date SecondDate
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.