Finding the most recently added/modified/created file

A new feature in Keyboard Maestro 8 is the ability to sort the files in the Folder Contents collection by a variety of different conditions:

  • alphabetically
  • by file size
  • by creation date
  • by modification date
  • by date added
  • by date downloaded
  • by date last used

And you can reverse the order as well. A nice outcome of this is that you can easily find the biggest/smallest newest/oldest file in a folder.

For example, these actions will find the most recently added file in your Downloads folder:

Keyboard Maestro Actions.kmactions (1.7 KB)

As a digression, see that warning triangle in the For Each action? If you click it, it will tell you that you are using a For Each action, but never referencing the variable (Path) within the For Each. Usually, this would be a mistake, but in this case we are really cheating a bit and just using the last value of the index variable. In many programming languages, the variable value after the loop would be unknown, but it is unchanged when you exit the loop in Keyboard Maestro. Perhaps better form (and getting rid of the warning) would be:

Keyboard Maestro Actions.kmactions (1.9 KB)

In any event, you can use this same technique to easily find the first or last file by any of the different conditions which can be an easy way to determine the file you want a macro to operate on.

7 Likes

Sorry to reply to such an old post, but how would you sort while ignoring a specific file type?

I'm trying to find the oldest audio file in a directory (by date added), but I have a ReadMe.txt that's the oldest added and is getting in the way.

You can add conditions. For example:

56-pty-fs8

Sorted List with Condition.kmmacros (4.0 KB)

3 Likes

What do you put in the "does not match" field if the file you are trying to exclude is a folder (not a file)?

Use the Path condition to test whether a file or folder does or does not exist at a specific location.

image