Triggered by "This folder"

I would like to be able to keep the most recent version of a certain file extension and move all other files older than the most recent version to another folder. I usually use Hazel for all my folder monitoring but currently Hazel breaks down when there is mixed file types in a folder and can not achieve this.

I am finding it difficult to find a solution using Keyboard Maestro as well. I can use the action “Move or Rename a File” but it is a file extension I want to look for and not a specific file.

This might explain better what I am trying to do if this is still unclear.

Hey Skillet,

In the appended macro:

The AppleScript generates a list of paths of files in the designated folder sorted by date-added, ascending – so the most recent item is at the top of the list.

The first RegEx removes paths that DON'T have the file extension you want.

The second RegEx strips the most recent item off of the top of the list resulting list.

That leaves you with a list of file paths of the specific file type (in this case .txt) that excludes the most recent one.

From there you can just use a Keyboard Maestro action to move the files – probably a For Each action with the lines in variable <variable-name>.

(I have used a Display Text action to display the path list – it's up to you to generate the actions to move the files.)

You could do all this purely with Keyboard Maestro, but you'd have to do all your own testing.

  • Folder Trigger triggers...
  • Get file list.
  • Strip off paths with non-desirable file-extensions.
  • Repeat through the trimmed list of paths to determine the most recently added file using the Get File Attribute action.
  • Strip that path out of the list.
  • Then move any remaining files.

If I was doing this job I'd do it all in AppleScript, but this hybrid macro may be of more help to more folks.


Generic-Test 01.kmmacros (6.4 KB)

1 Like

How I would do this is something like:

Folder Trigger: file added
if %TriggerValue% has the right extension 
    For Each file in the folder
        if the file has the right extension
            and the file is not %TriggerValue%
        then move/color the file

Thanks Chris, I will work through what you posted, thanks for all the details I will see what I can figure out.

Sadly, Peter I usually get lost when you reply with these values, if there are posts where you type like this and then show a screenshot of a plug-in that equates to what I would add I could figure how these translate back to Keyboard Maestro better. Verbally all that makes sense though and would be about what I would type if I never knew Keyboard Maestro exsisted. Connecting the dots is where it is not clear. I am not at all trying to be mean or disrepectful, I am always apprcetive of any help I can get, since any reply is out of the goodness of your heart!

Here is a macro that when you add a .h file to ~/FolderA will move all the other .h files to folder ~/FolderB.

Note that it will not cope with you adding two .h files at the same time.

It follows exactly what I wrote above, but as Keyboard Maestro actions now.

Folder Single.kmmacros (4.2 KB)

1 Like

Thanks a bunch Peter, so helpful, I will plan on going through it tomorrow and learning the rhetoric/connections.

I am still trying to digest this macro, how would one know to put “%Variable%Path%” in for the Move. Where did that name come from? I didn’t see anything explaining that on the help for this action folder.

It doesn’t make any sense to me why it is leaving just one .cpr file behind but it is.

Hey Skillet,

Path is simply a variable name Peter has used in his For Each action.

I discourage this usage in general, because it APPEARS that it might be a reserved word/name/object. The eyes travels right over it and sees it as path instead of as a variable representing a path string.

I tend to use “pathStr” or “pathVar” instead to make it more clear that I’m working with a variable.

More clear?


Path is a variable, and the %Variable%Path% text token can be used in any token text field to expand to the value of the variable.

Thanks Chris and Peter for clarifying this food for fodder I will keep reading and see what I can digest. You guys are major heros on this forum hands down!