Automatically Filing Files by Extension

Here is an example macro that shows how to automatically file images (jpg and jpeg extensions in this case) from the Desktop into the Pictures folder. The macro runs every hour. Obviously, you can adjust the macro any way you want to handle files with different extensions, process different folders, trigger by different means, etc.

Hi Peter

I am curios. Is the screenshot from Yosemite?
Looks like a different font for the text.

Kind regards Jimmy Hartington

Yes, I am running Yosemite (I switched early for a change).

1 Like

This is a very useful macro indeed! I have one challenge though:

The macro doesn’t move the files if there are files with the same name in the folder already. Is there an easy way to make it move them anyway? Maybe by appending a number to the filename?

Thanks a lot!

Very good. Wish I had seen it earlier. I spent a few hours debugging a script.

I also just discovered that Automator has this as a built in function which is even simpler,

Yes, Keyboard Maestro will not overwrite an existing file. It doesn't have any way to move it to a new unique name, so you would have to do this manually. Probably the easiest way would be to move the file to a new unique name first, and then try to move it to the original name (which would fail if the file already existed). Use the Get File Attribute action to get the original name, base named and extension of the file, and then do something like:

  • Set Unique Name to %Variable%Basename% - %ICUDateTime%yyyy-MM-dd-HH-mm-ss%.%Variable%Extension%
  • Move to ~/Pictures/%Variable%Unique Name%
  • Move ~/Pictures/%Variable%Unique Name% to ~/Pictures/%Variable%Original Name%

I want to copy the most recent file from a folder (of aText backups) to another folder. Doable with KM?

"most recent" by what definition? Most recently added? No.

Most recently modified or created, yes, Keyboard Maestro can do that by using the For Each action to scan the contents of the folder, and the Get File Attribute modification/creation date to get the modification/creation date of the file, and find the latest one. Something like:

Set Latest Date to 0
For Each file Path in Folder Contents
    Get File Attribute Creation Date for %Variable%Path% to variable This Date
    If Calculation This Date > Latest Date Then
        Set Latest Date to calculation This Date
        Set Best Path to text %Variable%Path
Display Text "The last created file is %Variable%Path%"