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?
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â:
Note:
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
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)
PS:
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.
[/quote]
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.
Yep, it works here, too.
Two little things:
A.
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)
B.
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"
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.