How to move or delete all files but the last one modified

Hello everyone!

I was wondering if someone could give me a hint on a macro I want to create, My team and I always work creating new versions every time we make any change on a project, at the end of the day we end up with a lot of old files that we manually move to a folder called _OLD.

I was thinking of using (for each in finder selection "move or delete"), but I don't know how to tell keyboard maestro to move everything but the latest file modified.

Thank you in advance,

the final folder can look like this and at the end of the day we can end up with 30 or more folders with this same look, which is tedious to clean them all manually!


Here a pure KM solution:

Count Files and Move All but the Latest One <D5F6 200222T062331>.kmmacros (6.8 KB)

Set your actual folder paths in the green actions.

First, the macro counts all .cpr files in the source folder. Then it moves files (sorted by modification date) while counting backwards from the number of found files, until the counter reaches 1.

1 Like

Here a variant via shell script:

Move All Files but the Latest One (Shell Variant) <1946 200222T074935>.kmmacros (2.5 KB)

ls builds a list of all .cpr files, sorted by modification date, tail removes the first one from the list and passes the rest to mv, which moves them to the destination folder.

Inspired from here:

And, finally, an alternative pure KM variant:

Move All Files but the Latest One (KM Alternate Version) <9064 200222T082117>.kmmacros (5.0 KB)

This one works similarly to the shell script above:

It builds a list of .cpr files by modification date, removes the first line from the list and moves the rest to the destination folder.

Guuys, sorry for the late response!

THANK YOU VERY MUCH, all of them work and me and my team are using them a lot, I even modify some of them to do other stuff related and it has saved us tons of time. thank you all!