I need to move a bunch of files to a folder but it needs to be the most current for this specific app. The folders are named like this “AppName 4.0.3”, or AppName “4.0.1” etc. The user will probably have a few different versions in their Library. I just want to find the most recent one, in this case that would be 4.0.3, and continue from their. I’m assuming this would be sorting by name but for some reason i can’t get it to work.
Here is a little DragonDrop code to return the latest file in a folder full of stuff. It will only test files for the latest date, excluding sub-folders from the tests. It converts the modification date into an integer for comparison purposes. However, if a file has come from a PC with a bogus creation date it seems to fail the test.
From there you can test the latest file reported here for it’s type/creator and act accordingly. Maybe you can do the type/creator test first to reduce the number of files you need to do the mod date test on. I’m not sure if this helps, but you will have to do a date test somewhere I think.
property monthsez : {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} as list
on open DragonDrop
if (count of items in DragonDrop) > 1 then error number -128
if (character -1 of (DragonDrop as string)) is not ":" then error number -128
tell application "Finder"
set DisFolderName to DragonDrop as string
set DaFileList to list folder DragonDrop
if (count of items in DaFileList) < 1 then
display dialog "There no files in this folder."
error number -128
end if
set DaLatestFile to alias ((DisFolderName & item 1 of DaFileList) as string)
set LatestModDate to 0
repeat with x from 2 to count of items in DaFileList
if (character -1 of ((item x of DaFileList) as string)) is not ":" then
set DisModDate to modification date of alias ((DisFolderName & item x of DaFileList))
tell me to set DisModDate to makeJDT(DisModDate as string)
if (DisModDate as number) > (LatestModDate as number) then
set DaLatestFile to alias ((DisFolderName & item x of DaFileList) as string)
set LatestModDate to modification date of DaLatestFile
tell me to set LatestModDate to makeJDT(LatestModDate as string)
end if
end if
end repeat
end tell
if (character -1 of (DaLatestFile as string)) is ":" then
display dialog "There no files in this folder . . ." & return & "Only sub-folders"
error number -128
end if
tell me
activate
display dialog DaLatestFile as string
end tell
end open
on makeJDT(curdate)
set dow to word 1 of curdate
set curyear to word 4 of curdate
set curyr to (characters 3 thru 4 of curyear) as string
set curday to word 3 of curdate
set curmonth to word 2 of curdate
set monthnum to 0
repeat with x from 1 to (number of items in monthsez)
if item x of monthsez is curmonth then set monthnum to x
end repeat
set hrs to word 5 of curdate
set min to word 6 of curdate
set Sex to word 7 of curdate
set ampm to word 8 of curdate
if (ampm is "PM" and hrs is not "12") then set hrs to hrs + 12
if (ampm is "AM" and hrs is "12") then set hrs to 0
set jmonthnum to monthnum
set jcurday to curday
set jcuryr to curyr
set jhrs to hrs
set jmin to min
set jsex to Sex
if (monthnum as integer) < 10 then set jmonthnum to "0" & (monthnum as integer)
if (curday as integer) < 10 then set jcurday to "0" & (curday as integer)
if (curyr as integer) < 10 then set jcuryr to "0" & (curyr as integer)
if (hrs as integer) < 10 then set jhrs to "0" & (hrs as integer)
if (min as integer) < 10 then set jmin to "0" & (min as integer)
if (Sex as integer) < 10 then set jsex to "0" & (Sex as integer)
set JDateTime to (curyear & jmonthnum & jcurday & jhrs & jmin & jsex) as string
return JDateTime
end makeJDT
I hope this helps.
Lemeeno
Jeff
thanks Jeff, it’s certainly very useful.
If the folders are all stored in the same place, you could try my FinderSort() handler here in ScriptBuilders. Sort the folders by name and (assuming your naming scheme) the last one in the list will be the latest. As my code is rather long, you may prefer to load it separately as a library:
set FS to (load script file "path:to:FinderSort:script:file") -- interpret as necessary
tell application "Finder"
set storageFolder to folder "path:to:folder:containing:the:folders:"
set FinderRef to a reference to (every folder of storgeFolder whose name begins with "AppName")
set latestAppNameFolder to item -1 of (FS's FinderSort(FinderRef, "name"))
end