Select by File Type and Move Them to a New Folder

Wow. Thanks for all your hard work!

One more question... :slight_smile:
How would I alter it if i wanted more than one file type consolidated? I usually have 2 different file extensions that my audio program creates and I want to put them both in one folder.
Again thanks for all your help!

I tried to duplicate it and run it again with a different extension in the 'zip' line but it throws an error...

Consolidate Files by File Extension ⇢ Shell Version v2.00 edit.kmmacros (8.0 KB)

Your macro works perfectly fine for me, although it creates two different folders – one for each file extension.

I suspect you're having a problem with your trigger or your group activation.

Put the test macro in an always active global or Finder-only group, and give it a simple trigger like F1 for testing.

I'll get to this in a bit – I'll have to make the script a trifle less complex, which will make it a bit more complicated for an inexperienced user.

-Chris

Here you go.

-Chris


Consolidate Files by File Extension ⇢ Shell Version v3.00.kmmacros (6.2 KB)

Works perfectly! Thanks for your time and help with this! Happy new year!

How would you keep JUST the most recent file and move all others? Is it pretty simple cause I don't want to keep bugging you but that's the last thing I was hoping to do with KM. I thought if I could get this far I'd be able to get it on my own but I know zip about shell scripting :slight_smile:

[edit]

It would only be 1 file type for this action though

Another option you have is to use Hazel, which is built specifically to monitor files and folders and then act on them based on criteria you set up. Many KM users also employ Hazel, and the two apps work well together.

I have several Hazel actions that move and/or rename files after a period of time. For instance, I take a lot of screenshots, so I have Hazel actions that keep screenshots I've taken in the last 48 hours on my desktop, but anything older gets moved to an archive and sorted into dated folders (year and month). Other types of files that land on my Desktop, Downloads, and dozens of other folders have similar do-something-if-file-is-older-than actions.

Thanks I'll look into it :slight_smile:

Hey @Five18,

Not simple with Keyboard Maestro or AppleScript – but doable.

Yyou wanted a version that left the most recent pair of files in place.

This was a bit tedious to do with vanilla AppleScript, but not too bad.

Easier to do with the shell I think, but I'm more familiar with AppleScript.

-Chris


Consolidate Files by File Extension ⇢ AppleScript Version v1.00.kmmacros (8.7 KB)

Hazel is a great utility and muy powerful, but at $42.00 U.S. it is more expensive than Keyboard Maestro.

Nevertheless – if it flicks your bic then more power to you. Hazel is made for jobs just like this one.

-Chris

Hey @Five18,

Okay. I've given you an AppleScript solution for leaving the most recent file-pair in place, but I couldn't resist doing the job in the shell for practice.

It was quite fiddly to get it running on Bash 3.2.57(1), which is stock on Sierra and still stock on Mojave! That silly thing goes back to 2007.

I have Bash 5.0.17(1) on my system – installed via MacPorts (although Homebrew is probably easier to use).

I wonder what's stock on Big Sur...

In any case – I've got this running on both my macOS 10.12.6 Sierra and my macOS 10.14.6 Mojave systems.

I've commented the Bash script reasonably thoroughly, but I haven't gone out of my way to make it easy for the user to modify.

-Chris


Consolidate Files by File Extension ⇢ Ignore Most Recent Pair ⇢ Shell Version v4.00.kmmacros (11 KB)

Thanks for all of your hard work on this! I only needed the 1 file type (cpr) saved ion the most recent but I'm sure I can figure out how to alter it (hopefully haha)

Thanks again!

Well... Maybe not LOL

Hmm... I wonder how I got the idea we were dealing with .csh & .bak files...

<shrug>

:sunglasses:

Try this. It now operates only on .cpr files.

Consolidate .cpr Files by File Extension ⇢ Ignore Most Recent ⇢ Shell Version v4.01.kmmacros (11 KB)

-Chris

Brilliant! This is exactly what I was looking for! Thank you!

1 Like

This is so great! Thank you once again @ccstone.

In the past, I've learned a lot and "Frankensteined" together some useful macros for myself using your scripts. With this specific macro, I don't have the knowledge to accomplish what would be helpful for me. Using this macro, I get close, but no cigar, and I don't want to screw things up!

For me, this scenario would be ideal:

If within Finder, I could point to (highlight) a root folder that contains many other folders, and have KM search all of the folders within the selected folder for specific file types, then copy those files to a new folder within the originally selected folder.

How would one accomplish this?

Hey Ron,

This is a fairly straightforward task and only requires Keyboard Maestro native actions.

The only part that's a little tricky is where I test for the file extension with a regular expression.

-Chris


Finder ⇢ Extract Files of Given Types from the Selected Folder v1.00.kmmacros (9.1 KB)

Macro-Image

Keyboard Maestro Export

@ccstone to the rescue once again! Thanks so much, this is great.

Hey, Chris. I'm curious why you split the extension from the full file path rather than simply applying the regexp match (slightly modified) to the path itself. You must be accounting for some edge case where the results would be different. Yes? Curious what that is. Thanks. –Steve

Hey Steve,

I think the logic here is a tad more obvious for people not well acquainted with Keyboard Maestro and/or regular expressions.

File name extensions make more sense to most folks than complete path strings.

That said – as you mention – splitting the path is not strictly necessary for getting the job done.

-Chris

Thanks, Chris. Yes, absolutely true that your solution very clearly performs the operations specified in the problem. Without that extra step someone might well have asked a question like mine in reverse. Thanks for the response! –Steve

1 Like