How do I get all the files within subfolders and copy them all to another folder?

I have a bunch of folders with files in them. I want to take all the files and copy them to another folder. Which action would I use to do this?

1 Like

You can do it like this:

[test] Copy Contents of Several Folders.kmmacros (3.6 KB)

Set your paths in the green actions.

You can also set the source folders by making a Finder selection. In this case remove the first action and set the outer “For Each” action to “The Finder’s Selection”:


Any _sub_folders contained in the source folders will be copied as folders. If you want the content of the destination folder to be completely flat, then we have to do a slight modification:

[test] Copy Contents of Several Folders (flat).kmmacros (4.4 KB)

Those are pure KM solutions.

You can make the macro shorter for example by using shell scripts:

cp -R "$KMVAR_zzzSourceFolder"/ "$KMVAR_zzzDestination"

or for a flat result:

find "$KMVAR_zzzSourceFolder" -type f -exec cp {} "$KMVAR_zzzDestination" \;

Implemented like this:

[test] Copy Contents of Several Folders (with shell script).kmmacros (3.7 KB)


Thanks for the amazingly detailed response! I’m trying to test and decode this all now. I’ll report back.

Ok. So I think I get how this works regarding copying files inside of folders to a single different folder.

What I’m still having trouble with is how to copy the selected folders themselves to a new directory, but as I was typing this I think I figured out the issue. It’s that the “for each item in finder selection” by default adds a ‘/’ character to the end of the path which causes cp to copy the files within. I just filtered the variable using “Delete Path Extension” and that solved my folder copying issue using the shell script option.

Thanks again.

EDIT: I was wrong again. It turns out some of my issues stemmed from the fact that I didn’t put a ‘/’ character at the end of the destination of my newly created folder. Learning.

EDIT2: So after more testing, it’s actually a combination of both. I need to add a ‘/’ to my destination folder, and I need to filter the last slash of the folders I’m copying. Thank goodness this will save me literally seconds to minutes a day at work.

I’m not in front my Mac right now, but if you want to copy the folders instead of the contents it should be sufficient to remove the slash at the end of the source path in the ‘cp’ shell script, i.e. after the first variable.

I had explicitly added the slash because I thought you wanted to copy the contents, not the folders :wink:

But if you just want to copy the selected folders, then half of my posted stuff is completely unnecessary. I will post a simplified macro when I’m back.

OK, this is enough to simply copy every selected Finder item (folder or file) to a given destination directory:

[test] Copy Selected Items to a Folder.kmmacros (2.2 KB)


If we want to use a shell script, you have correctly noticed that the paths from the “Finder’s Selection” come with a trailing slash if they are folders, which changes the outcome of the ‘cp’ script.

But instead of “Delete path extension” you have to use the “Standardize path” filter, because “Delete path extension” will delete any extension. That is, regular files with an extension would not be copied and folders with a dot in the name would not be copied either. This should work:

[test] Copy Selected Items to a Folder (with shell script).kmmacros (2.7 KB)

For some reason this does not work properly when, for instance, I just select one folder and activate the hotkey, it will copy just the files inside of the folder to the destination. If I select two folders, it will copy only the files inside the first folder and then the whole second folder as-is.

Hope that makes sense. I think this is why I’m confused on how this works and why I had originally made two different topics.

I was updating my last post when you posted your reply. It now has a working shell script version, too.[quote=“elstie, post:7, topic:7018”]
For some reason this does not work properly when, for instance, I just select one folder and activate the hotkey, it will copy just the files inside of the folder to the destination.

I guess you are referring to macro #1 of my latest post. It works fine here, also with just one folder selected, or with two folders selected.

Yeah. I was replying to your pre-edited post. But this is honestly very bizarre. Even with the shell script, it still copies only the contents of the first folder selected. I'm wondering now if this is a bug or I'm missing something tiny that I can't figure out.
Here's my script currently. I disabled actions where I was testing other versions.

Can you please upload the macro?

(Select the macro in KM Editor, then KM Editor > File > Export Macros…, then drag the exported macro file into the edit window of the forum.)

I’ve just seen, you have a typo in the second action: %Variable%, not %Variables%.

You have to make sure that the destination folder exists on disk, before running the macro.

It just occurs to me that maybe you are not aware that you can download my posted macros by clicking the download link.

So I propose you download macro #1 from that post and try again if it works. Just adapt the path in the green action to an existing folder on your disk, and don’t add any complications like date/time folder names.

Thanks for pointing out that I can download the macros, and thanks for catching the typo. I think the typo was the problem in this case. The goal of the macro is to copy folders from a flash drive into a dated and timed folder in Downloads. It looks like the key may have been 1) the typo and 2) creating the folder first before starting the copy.

I've attached what I think is the final bug-free macro that works right.

COPY TO DATED DOWNLODS FOLDER testing.kmmacros (2.8 KB)

Yep, it works here, too.

Two little things:


The Create Folder action has a little pitfall: it doesn’t create any missing intermediate folders. That is, you can only create a new folder in a folder that already exists.

For example, if the “Downloads” folder does not already exist, then the action will fail to create the destination folder.

So, better use this action instead:

This will create the destination folder and all intermediate folders if necessary.

Here the complete macro: COPY TO DATED DOWNLODS FOLDER testing [rev].kmmacros (3.2 KB)


Maybe a matter of taste, but for copying the files I wouldn’t use a shell script action if it can be done equally well with a “native” KM action, in this case the Move/Copy File action.

Thanks for the revision. I downloaded your version with the shell script still being used to copy. But when I made the change to use KM's native action to copy files, I ended up with the same issue as before. Here's that script: COPY TO DATED DOWNLODS FOLDER testing.kmmacros (3.7 KB)

It works flawlessly, if you correct one error:

Look at your third action: you have written cp -p "$KMVAR_toDOWNLOADS"

instead of

mkdir -p "$KMVAR_toDOWNLOADS"
1 Like

HAHA…dammit…great catch.

Thanks for all your help, man. This has been a very informative and eye-opening troubleshooting session.

Hint: you can copy/paste actions between macros with ⌘C and ⌘V. So you don’t have to retype anything => less typos.

1 Like