How to Identify the Subfolder With the Most Recently Written File?

Hey Jimmy,

As has been noted your code is not recursive.

It works fine for a relatively small directory, but it gets progressively slower as the number of files in the directory grows.

A folder with 500 files in it takes nearly 10 seconds to process on my system.

You can make your code recursive by using entire contents:

set targetFolder to alias ((path to documents folder as text) & "Code_Projects:Script_Debugger:Saved-Sessions:")

tell application "Finder"
   set latestFile to the last item of (sort (get files of entire contents of targetFolder) by modification date) as alias
end tell

set latestFilePath to POSIX path of latestFile

But entire contents has always been grossly slow, and I wouldn't recommend it.

Note that I've pulled the POSIX Path coercion out of the Finder-tell-block. Apple engineers recommend that all calls to osaxen (in this case StandardAdditions) be placed outside of application-tell-blocks.

For a much faster lookup see this script here:

Finding the last added item

Note that there is a list of keys at the end of the script that let you look for creation-date, modification-date, and others.

This script is non-recursive, but I will post one soon that is recursive.

-Chris