[SOLVED] Find specific files inside folders AND sub folders and then copy them

I think I read about this somewhere, that KM can't dig inside subfolders, right?

Here's what I would like to achieve:
I want to have KM use a certain folder as the "root" and then dig inside that folder to find all files that meet a certain criteria, for example all files that include the word "ABC" and are also .mp3 and then create a copy of those files to another folder.

Is this possible with KM?
I also have Hazel and this can be done there, but I would have to keep opening the app, enable the folder I use as root, hit Run Rules, let Hazel do its thing (at this point I have to wait for it to finish), then disable the folder again and close the app.

With KM I could just assign a shortcut and let it do its thing in the background.

Is it possible?

The trick is to set the For Each Action to the Items in Directory and use "recursively". That will act on every item (even in subfolders) in that Directory.

Then, in that For Each loop, use an If Then Else with whatever conditions you want to copy the files that meet your criteria to another folder.


I'm doing something obviously wrong, but how do I get the full path of the variable, which is what's causing this error:

2023-08-31 21:35:21 Action 13996338 failed: File action failed because source is not a full path %Variable%Local__VarName%
2023-08-31 21:35:21 File action failed because source is not a full path %Variable%Local__VarName% in macro “For Each” (while executing Copy File “%Variable%Local__VarName%”).

Test Recursively Macro (v10.2)

Test Recursively.kmmacros (21 KB)

Keyboard Maestro Export

I would guess that you have mistyped Local_VarNameABC as Local_VarName in this Action:

1 Like

See...? So obvious... I guess I'm just too tired already
Thank you so much for your help. Will test some other variations tomorrow, more complex stuff and see how it goes. :raised_hands:

1 Like

Also, one other thing to be aware of. If you want to test if the file name contains "hobbits" you can first extract the file name to a variable. If you simply test Local_VarNameABC for "hobbits" it will look at all the text in the whole file path and not just the name.

You can use the same technique to test say if the file extension is .mp3

Test Recursively v2.00.kmmacros (22.3 KB)

Click to Show Image of Macro


Nice macro, gentlemen! Since I saw many useful uses for this macro, I tested it.

I noticed hat if the same file (e.g. user.dic) exists in several folders, only one version is collected.

I'm not sure what's clever here: add a counter to the file names or create new (numbered) subfolders and leave the file names as they are ... The latter sound a little strange in this context, since you would be creating a new folder structure ;).

I suppose that makes sense as Keyboard Maestro won't overwrite an existing file. And I suppose it depends on the actual use case whether that is wanted or not wanted.

Definitely the basic example shown here could be modified if duplicate copies were wanted.