Move Matching Files to a Named Folder?

Hello KM Community!

I've been reading this forum for a while, and it has been of great help!

I need help with the following, and I'm not sure how to go about it.

The problem

I work in the music industry and I need to sort and folder a bunch of files by instrument name, so for example, all files that contain "Fl1" need to go into a folder named "Flute 1". This is done at the end of every project to send the specific parts to each musician, when it's 20+ scores it becomes very tedious and repetitive.

Is there a way to dump all the files in a folder, and run a macro that finds every file containing the filename "Fl1" and it moves it into a folder that it creates named "Flute 1".

Of note, because each score is different, the part before "Fl1" is always different, because that's where the song name goes. So it would need to filter files like "A Very Sad Song Fl1" or "Where Is My Macro Fl1" to a folder called "Flute 1"

I'm trying to figure a way, but it's beyond me and I need help.

Thanks in advance for the help and let me know if I was clear in what I'm trying to do.

All the best.

for F in $(ls | cut -d- -f1 | uniq); do
    mkdir "${F}" && mv "${F}"-* "${F}"
done

This script will move files with same prefix to folder named with prefix-string

1 Like

This is possible in Keyboard Maestro using the For Each Action for a Finder Selection. To use it, you would select all the files that are in the folder and run the Macro. Files that end with "FL1" get copied to a new folder called "Flute 1" and files that end with "FL2" get copied to a new folder called "Flute 2". It is set to "copy" rather than "move" for safety (but it could be set to "move").

For explanation, "File name" is the name with extension and "Base name" is the name without the extension. So to test whether the name ends with "FL1" we use the "Base name", but when copying the file to the new location we use the "File name" as we want to keep the extension...

I assumed "FL1" and "FL2" are always at the end of the filename as in your examples but for course you could change the search to "contains" rather than "ends with" if that is not always the case.

And the path where the new folders are being created could be a Variable to make the Macro more practical for real use.

Another approach would be to use the App Hazel which is designed for this kind of file/folder stuff and might be easier to maintain the rules going forward.

EXAMPLE Copy Files to Folders Based on ending of name.kmmacros (5.1 KB)

Click to Show Image of Macro

.

1 Like

I love Keyboard Maestro but I also use Hazel. As was already mentioned:

Hazel will do all that with a few simple rules.

2 Likes

Hey Zabodon!

Thank you so much for this! I'll give it a try! How do I make the folder where they are created be a variable?

I'll also take a look into the Hazel App, thanks for the tip!

1 Like

Well, instead of a path like this:

~/Desktop/Flute 1

You would save ~/Desktop in a Variable called say, LOCAL__Path

And then you could quote your path like this:

%Variable%LOCAL__Path%/Flute 1

The advantage being that you only have to set LOCAL__Path once in your Macro and don't have to type the actual path in every single Action.

And to save your files to a different path you would set the Variable LOCAL__Path to that new path without having to edit the rest of the Macro.

Yes, I would say Hazel is a great App to have anyway and this seems a good use for it.