Friday, December 15, 2017

#1 2007-10-19 11:35:57 pm

Scriptic
Member
Registered: 2007-08-01
Posts: 14

Best way to get most recent file in a folder

I'm having trouble figuring this one out.  I could brute- force the issue, but I think there must be a more elegant way to solve this.

I would like to find the most recent file in a given folder, by creation date.  I can get all the creation dates of the file items, but then I have to somehow associate those most recent dates with the file's actual name (so I can open it).  It's quite ugly -- at least the way I do it it's ugly...

I tried ordering the Finder window by column view sorted on Creation Date, but I can't get the particular folder window I want to open.  I thought the first item in that list would be the one I want, but I can't get it to work.

I'd very much appreciate any help anyone can offer.

Offline

 

#2 2007-10-19 11:48:08 pm

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: Best way to get most recent file in a folder

Try something like this:

Applescript:

choose folder with prompt "Find newest file in this folder:"
set sourceFolder to result

tell application "Finder"
   sort (get files of sourceFolder) by creation date
   -- This raises an error if the folder doesn't contain any files
   set theFile to (item 1 of result) as alias
end tell

Note that the Finder's `sort` command requires Mac OS X v10.4 or later.


Filed under: Finder

Offline

 

#3 2007-10-20 12:57:51 am

Scriptic
Member
Registered: 2007-08-01
Posts: 14

Re: Best way to get most recent file in a folder

Bruce Phillips wrote:

Try something like this:

Applescript:

choose folder with prompt "Find newest file in this folder:"
set sourceFolder to result

tell application "Finder"
   sort (get files of sourceFolder) by creation date
   -- This raises an error if the folder doesn't contain any files
   set theFile to (item 1 of result) as alias
end tell

Note that the Finder's `sort` command requires Mac OS X v10.4 or later.


Darn...  Any options for 10.3.9?  Maybe I'll have to upgrade in two weeks...

If I wanted to pass a folder directly, instead of requiring human intervention, could I do that with something like:

set sourceFolder to "HD:Top Level Folder:Sub Folder:One More Sub Folder:Target Folder"

Model: PowerBook PowerPC G4
AppleScript: 1.9.3
Browser: Firefox 2.0.0.7
Operating System: Mac OS X (10.3.9)


Filed under: Finder

Offline

 

#4 2007-10-20 05:55:23 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4454

Re: Best way to get most recent file in a folder

Scriptic wrote:

Darn...  Any options for 10.3.9?  Maybe I'll have to upgrade in two weeks...


If you don't want to upgrade, or can't, there's a stop-gap Finder sort script of mine in the ScriptBuilders section of this site.

I keep the "FinderSort().scpt" file in a folder call "Libraries" in my "Scripts" folder and load it into any of my Jaguar scripts that need it. An adaptation of Bruce's script to use it like this would be:

Applescript:

-- Load the FinderSort() script file into this script.
set FSort to (load script file ((path to scripts folder as Unicode text) & "Libraries:FinderSort().scpt"))

choose folder with prompt "Find newest file in this folder:"
set sourceFolder to result

tell application "Finder"
   -- FinderSort() needs a Finder reference, not a list.
   set f to a reference to files of folder sourceFolder
   FSort's FinderSort(f, "modification date")
   -- This raises an error if the folder doesn't contain any files
   set theFile to (item 1 of result) as alias
end tell

…

If I wanted to pass a folder directly, instead of requiring human intervention, could I do that with something like:

set sourceFolder to "HD:Top Level Folder:Sub Folder:One More Sub Folder:Target Folder"


The Finder sometimes accepts a path string instead of a proper file or folder reference, but doesn't in this case. It would be better to coerce the path to an alias:

Applescript:

set sourceFolder to "HD:Top Level Folder:Sub Folder:One More Sub Folder:Target Folder" as alias

… and/or to put the word 'folder' in front it inside the Finder 'tell' block:

Applescript:

tell application "Finder"
   sort (get files of folder sourceFolder) by creation date
   -- This raises an error if the folder doesn't contain any files
   set theFile to (item 1 of result) as alias
end tell


NG


Filed under: Finder

Offline

 

#5 2007-10-20 05:57:53 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Best way to get most recent file in a folder

Hi,

this should work also in Panther

Applescript:

set sourceFolder to "HD:Top Level Folder:Sub Folder:One More Sub Folder:Target Folder:"
tell application "Finder" to set theFile to 1st file of folder sourceFolder whose creation date is (last item of my bubblesort(get creation date of files of folder sourceFolder))

on bubblesort(array)
   repeat with i from length of array to 2 by -1 --> go backwards
       repeat with j from 1 to i - 1 --> go forwards
           tell array
               if item j > item (j + 1) then
                   set {item j, item (j + 1)} to {item (j + 1), item j} -- swap
               end if
           end tell
       end repeat
   end repeat
   return array
end bubblesort


regards

Stefan


Filed under: Finder

Offline

 

#6 2007-10-20 05:18:54 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4454

Re: Best way to get most recent file in a folder

Or, indeed:

Applescript:

set sourcePath to "HD:Top Level Folder:Sub Folder:One More Sub Folder:Target Folder:" as Unicode text

tell application "Finder" to set {fileNames, modDates} to {name, modification date} of files of folder sourcePath

set n to 1
set latestDate to beginning of modDates

repeat with i from 2 to (count modDates)
   set thisDate to item i of modDates
   if (thisDate comes after latestDate) then
       set n to i
       set latestDate to thisDate
   end if
end repeat

set theFile to alias (sourcePath & item n of fileNames)


NG


Filed under: Finder

Offline

 

#7 2007-10-21 06:43:26 am

Scriptic
Member
Registered: 2007-08-01
Posts: 14

Re: Best way to get most recent file in a folder

Bruce, Nigel, and Stefan,

Thank you guys so much for your help.  It's made a huge difference in my script.

I was reading over and trying to understand Stefan's solution (along with Nigel's), when Nigel made his latest response.  It is so elegant, so simple, and so beautifully written -- it's a work of art.

Thanks again for everyone's help!

Offline

 

#8 2013-12-12 12:39:36 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 131

Re: Best way to get most recent file in a folder

Hey,

This seemed simple enough but am having no luck getting it to work.
Just need to get the most recent file in the folder.

Applescript:

set sourceFolder to "Macintosh HD:Users:TV:Sites:ss2"

tell application "Finder"
   sort (get files of sourceFolder) by creation date
   -- This raises an error if the folder doesn't contain any files
   set theFile to (item 1 of result) as alias
end tell

The error it returns is:  Can’t get every file of "Macintosh HD:Users:TV:Sites:ss2".

There are two files in the folder with different creation dates.
I'm running 10.5.8

Any help greatly appreciated.

Thanks,

Carl

Offline

 

#9 2013-12-12 01:33:56 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

Either you have to turn sourceFolder into an alias, or you'll have to stick "folder" in front of sourceFolder in the Finder tell block

Last edited by McUsrII (2013-12-12 01:34:22 am)

Offline

 

#10 2013-12-12 10:05:31 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 131

Re: Best way to get most recent file in a folder

Tried that and a few variations but still no go.

Applescript:

set sourceFolder to "Macintosh HD:Users:TV:Sites:ss2" as alias

tell application "Finder"
   sort (get files of folder sourceFolder) by creation date
   -- This raises an error if the folder doesn't contain any files
   set theFile to (item 1 of result) as alias
end tell

Getting this error:  Can’t make item 1 of {} into type alias.

Appreciate the help.

Thanks,

Carl

Offline

 

#11 2013-12-12 10:21:15 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

I fixed that one for you, but now, if you have some more stuff to do with theFile, then you should do that within the if block. (Just mentioning it.) smile

Applescript:

tell application "Finder"
   try
       set sortedList to sort (get files of folder sourceFolder) by creation date
   end try
   if sortedList is not {} then
       set theFile to (item 1 of sortedList) as alias
   end if
end tell

Last edited by McUsrII (2013-12-12 10:22:18 am)

Offline

 

#12 2013-12-12 12:49:42 pm

ckeyes888
Member
Registered: 2010-04-27
Posts: 131

Re: Best way to get most recent file in a folder

Thanks a bunch for the fix. Works great. I see now my security camera app is creating new
folders within the ss2 folder each time I set it to record a video file.

Have to try to work that out as well. Might you know if it's possible to search subfolders as well within ss2?

Thanks,

Carl

Applescript:

set sourceFolder to "Macintosh HD:Users:TV:Sites:ss2"

tell application "Finder"
   try
       set sortedList to sort (get files of folder sourceFolder) by creation date
   end try
   if sortedList is not {} then
       set theFile to (item 1 of sortedList) as alias
       return theFile
   end if
   
end tell

Last edited by ckeyes888 (2013-12-12 01:07:02 pm)

Offline

 

#13 2013-12-13 12:18:34 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

I think I know of a couple of ways to get the lastest modified file in a folder hierarchy.
We can't use finder for that, so first I want to know, (and I'm not even sure if that is the way I want to go) does ASOC work for you? -I believe you use the indigo server, what I want to know, is whether you use Mavericks, and then if  the ASOC statements within an "indigo" script, together with ASOC libraries works for you.

If you don't know, and don't care to find out, then that is fine, but this is the fastest way to go.

Save the script below as a Script Bundle with AppleScript editor, click the drawer icon and hook off for ASOC library, with the name ASOCTest, in your ~/Library/Script Libraries folder. (Make it if it isn't there).

Applescript:

use AppleScript version "2.3"
use scripting additions

on testit()
   tell (path to frontmost application as text)
       tell me
           display dialog "it works"
       end tell
   end tell
end testit

Now, stuff the script below into an indigo script window, and try to run it.

Applescript:

use AppleScript version "2.3"
use scripting additions
use asoct : script "ASOCTest"


asoct's testit()

If you now see a dialog box, then we can use ASOC

Last edited by McUsrII (2013-12-13 12:27:32 am)

Offline

 

#14 2013-12-13 12:41:19 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 131

Re: Best way to get most recent file in a folder

No doubt that would be an excellent way to go but my indigo server is
running 10.5.8. I have used ASOC for other tasks on other machines and it does work very well.

Thanks,

Carl

Offline

 

#15 2013-12-13 12:44:33 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

OK.

What I wonder is, is if there always are just going to be a small number of files there, and that the folder hierarchy is not deep, like 3-4 levels or deeper.

Then I think we can implement a solution with Finder first, to see if that works out, or if the consisistency smile of your root folder is different, then we go straight for a do shell script involving mdfind and sort.

Offline

 

#16 2013-12-13 12:48:51 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

Independently of how you reply to the former message, I'll be back in some hours with a solution.

Offline

 

#17 2013-12-13 02:58:31 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

Please see if this works for you. The script below is supposed to work under OS X 10.5, and won't work under Mavericks, since .DS_Store files are visible, and the files are sorted in the opposite direction.

Applescript:

set sourceFolder to "Macintosh HD:Users:TV:Sites:ss2"
tell application "Finder"
   return item 1 of (sort (every file of entire contents of folder sourceFolder) by modification date) as alias    
end tell

Last edited by McUsrII (2013-12-13 09:55:43 am)

Offline

 

#18 2013-12-13 08:23:08 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 131

Re: Best way to get most recent file in a folder

That one is working but it does return the oldest file, not the newest.
The source folder may contain a number of folders, but would never be more than 1 folder deep.

Many thanks,

Carl

Offline

 

#19 2013-12-13 09:27:09 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Best way to get most recent file in a folder

Hello cheyes888

I'm puzzled because here, McUsrII's script returns correctly the most recent file.
Im running the current Mavericks.

Yvan KOENIG (VALLAURIS, France) vendredi 13 décembre 2013 15:26:52

Online

 

#20 2013-12-13 09:29:22 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4454

Re: Best way to get most recent file in a folder

Hi.

1. The Finder doesn't return ".DS_Store" files, so the repeat's not necessary. The script object's not necessary in any case.

2. The result of sorting by dates with the Finder has recently changed, probably by accident, since it hasn't been announced. In Mavericks, dates are sorted from oldest to most recent, rather than vice versa as before. McUsrII's script gets the most recently modified file in Mavericks or the earliest otherwise. If you're going to use the Finder's 'sort', you either have to test the system and take the appropriate action, or get both the first and last items in the list and see which is younger.

Last edited by Nigel Garvey (2013-12-13 09:38:42 am)


NG

Offline

 

#21 2013-12-13 09:43:48 am

ckeyes888
Member
Registered: 2010-04-27
Posts: 131

Re: Best way to get most recent file in a folder

Thanks all. I'm running 10.5.8 so, if I have this correct, I need to mod the script in some way to
get the most recent?

Carl

Offline

 

#22 2013-12-13 09:45:03 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Best way to get most recent file in a folder

Hello Nigel

I tested under Mavericks after adding an instruction logging the entire list.

Here is a short subset :

document file .DS_Store of folder 2345 mises à jour of folder tempo of folder Documents of folder yvankoenig of folder Users of startup disk, document file .DS_Store of folder tempo of folder Documents of folder yvankoenig of folder Users of startup disk, document file

Yvan KOENIG (VALLAURIS, France) vendredi 13 décembre 2013 15:44:38

Online

 

#23 2013-12-13 09:57:10 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

I have hopefully fixed the script in post # 17 so it doesn't consider .DS_Store files, and respects the sort order by modification date as of Mac Os X Leopard.

Offline

 

#24 2013-12-13 09:57:50 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Best way to get most recent file in a folder

Here is a version behaving correctly on the different systems :

Applescript:

set sourceFolder to (path to home folder as text) & "Documents:tempo:" #"Macintosh HD:Users:TV:Sites:ss2"
script o
   property l : {}
end script
tell application "Finder"
   set o's l to sort (every file of entire contents of folder sourceFolder) by modification date
   # log o's l
   repeat with i from (count o's l) to 1 by -1
       if name of item i of o's l is not ".DS_Store" then
           set maybe1 to (item i of o's l)
           exit repeat
       end if
   end repeat
   repeat with i from 1 to (count o's l)
       if name of item i of o's l is not ".DS_Store" then
           set maybe2 to (item i of o's l)
           exit repeat
       end if
   end repeat
   if modification date of maybe1 < modification date of maybe2 then
       return maybe2 as alias
   else
       return maybe1 as alias
   end if
end tell

I kept the script object even if it's not really useful in this case.

Yvan KOENIG (VALLAURIS, France) vendredi 13 décembre 2013 15:57:44

Last edited by Yvan Koenig (2013-12-13 10:08:51 am)

Online

 

#25 2013-12-13 10:00:50 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Best way to get most recent file in a folder

Hello.

I dare say the script object is useful if you have to reference the items of the returned result of the sort command for sifting out the .DS_Store files.

That is under the assumption that it is faster to reference an item in a list that is referenced by a script property. If my assumptions are wrong, then please enlighten me. smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)