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

I have some text files (actually in TMX format, but I'm not sure if that's really relevant here) in this folder:

/Users/Hans/Dropbox/CT/TM/

File names like:

mueller.tmx
meyer.tmx
maier.tmx

How can I detect which file in the subfolder "TM" has the most recent change?

I have this AppleScript code, which gets the latest file from my downloads folder.

It needs to be adjusted to your path.

tell application "Finder"
	set latestFile to the last item of (sort (get files of (path to downloads folder)) by creation date) as alias
	set p to POSIX path of latestFile
end tell

I see that my original question wasn’t clear at all. Sorry for that.

I should have written:

From the subfolders:

/Users/Hans/Dropbox/CT/TM/Meyer
/Users/Hans/Dropbox/CT/TM/Mueller
/Users/Hans/Dropbox/CT/TM/Maier

The folder /Users/Hans/Dropbox/CT/TM/Mueller contains the most recently modified .tmx file. How to identify this folder.

Why doesn’t this work?

set dbPath to (path to home folder as text) & "Dropbox:"
set tmFldr to dbPath & "CT:TM:"

tell application "Finder"
	set latestFolder to the last item of (sort (get folders of (path to tmFldr)) by modification date) as alias
	set folderName to latestFolder's name
	display dialog "the folder's name is: " & folderName
end tell

Two things:

  • With your (path to tmFldr) I get an error. However, it works with get folders of folder tmFldr
  • AFAIK a folder’s modification date does not change when the modification date of a file in that folder changes. (It changes only when you add or remove a file from the folder.)

Hi Hans

Oh, so it is modified files in subfolders.
So will my script not help.

You can use Keyboard Maestro to scan a folder (or folders) using the For Each action and the Folder Contents collection. Add a collection for each folder if appropriate, or scan the parent folder.

You can use the Get File Attribute action to get the type of the file (to restrict it to File types), and also to get the modification or added date. Scan through and fine the latest date and save the date and the path.

At the end you’ll have the path of the latest (added or modified) file.

Thank you all!

I found this, to work with:

Great stuff to read and play with ... during the weekend.

I do have these macros in my examples, but I haven't looked closely at them, but I guess they would be a good start.

Examples Macros.kmmacros (9.7 KB)

Hey Hans,

Probably the simplest way is with a shell script.

Find Last Modified File in a Directory (recursive).kmmacros (3.4 KB)

This has one major pitfall. If you have .app files or any sort of bundle file (e.g. a directory the Finder shows as a file), the shell script will descend into it.

It runs in about 1/2 second on a directory with many subfolders and about 13,000 files and will be much faster on a smaller directory tree.

There's a way to stop find from descending into bundles, but it's a little complicated – and I don't have it at my fingertips. I'll post that later if I can find it without too much trouble.

I'll also post an AppleScriptObjC script later that will ignore bundles.

-Chris

2 Likes

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

Thanks, @ccstone

I replace this in my macro and save it for further reference.

Thank you, Chris!

1 Like

A post was split to a new topic: How to Find the Most Recent File of a Given Type in a Given Directory Tree