Copy Folder Structure and Keep Certain Files [SOLVED]

I'm trying to create a macro that allows me to copy folders and the whole structure, without then pasting the files, just the folders.
I found this thread and it's working:
https://discussions.apple.com/thread/250646517

NOTE: for those interested, the code that person shared has an issue. The quotes are "tilted" to the right, instead of being straight.
image
I don't know if there's a name for that. Thing is, Terminal was showing me an error and I couldn't understand why. When I fixed that and made all quotes straight, it started working.

Now this code does everything I need, except that it doesn't keep my folders' custom icons, because that's a hidden file inside the folder, called Icon?

Is there a way to include those hidden icon files so I can keep the icons as well?
I'm by no means a Terminal expert. Not even a novice. I pretty much just copy-paste things and if it works, all good, so have that in mind if you share some advice as I may not understand what you mean.

Thanks!

1 Like

It's a good idea to turn off Smart Quotes in macOS Keyboard Preferences to avoid issues in shell scripts.

Why not reply to that Apple forum thread with your folder icon question? Seems like a good way to get a quick answer.

1 Like

It's disabled:
image

But I copied the text from the thread and the text itself has them tilted. Since I don't know anything about Terminal and shell scripts, I couldn't see that those were wrong quotes. Then the error itself seemed like a normal thing, because I had no clue what that meant.

1 Like

It was my first option, especially because I wanted to let the OP know that the code was wrong with the tilted quotes. The thread is closed, so I can't reply.

1 Like

Is this a template structure that you use again and again, or do you want to do this with random directory structures you choose when the macro runs?

Random structures. So I would select any random folder in Finder and then run the macro. It’s not always the same folder (and sub folders)

Hey Danny,

This is never going to work the way you want, because it uses the shell – and the shell often does not manage custom Finder flags.

There is no way to copy a folder structure without also copying the files in it.

So... The only way I can see to get your desired outcome is to copy the entire folder and then delete the contained files.

-Chris

1 Like

I meant to add - using either the Finder directly or via Applescript...

I'm not sure whether the Keyboard Maestro copy action manages custom folder icons or not. I think it uses the File Manager, so it might and it would be worth testing.

Turns out it does. Example that let's you choose a source folder, set name/location for your duplicated structure, duplicates then deletes everything but folders:

Copy Folder Structure.kmmacros (4.7 KB)

Images

1 Like

I suppose the only drag would be if you're duplicating a folder structure with gigabytes of files in it. As Danny works with audio, I imagine that's possible.

@iamdannywyatt, why do you want to duplicate these folder structures in the first place and why are the icons so important? Maybe there's a different workflow that would remove the need to do this at all.

Yep, that's the big issue here.

I guess another approach would be to create the folders from scratch then GUI-script a Copy/Paste between each pair of Get Infos -- which sounds a bit cheesy. Otherwise I think custom icons are are combination of an x attribute on the folder itself and the ?Icon^M file (yes, the name has an old-style Mac line ending character at the end), so there might be an approach via that.

1 Like

That's not quite true. It's perfectly achievable with AppleScript. However, the OP seems to really want the folder icons to be mapped across to the new folder hierarchy as well, and that's the part that has me stumped without going the ASObjC route.

Will give it some thought.

As mentioned on my post, I was able to make it work using the code they provided on that articled I shared. The issue is not that it doesn't work (which is does and I'm using it), the issue is that I would like to still keep some of the files when they are present.

As @noisneil mentioned, this macro would be a bit cumbersome when the folders have gigabytes of data, which sometimes they do.

Also, as mentioned on my original post, I was able to make it work with that Terminal code they shared on the article I liked to. So the issue is that I just can't keep the custom icons, so I was hoping maybe someone here who's good with Terminal (I guess that's what you would call the "shell script"?) could use that code and filter some files. Here's the original code, just in case:

ls -R | grep :$ | sed 's/\.\/\(.*\):$/\1/' | \
while read thisFolder; do mkdir -p "CopiedFolderStructure"/"$thisFolder"; done

Why do the icons matter and why are you duplicating the folder structure in the first place?

Sometimes I just need to copy a certain structure (like a template) and use it somewhere else. For example right now I want to start cleaning my disk and only keep files I truly need so I want to copy my main folder's (called "My Files") structure to a different location and start dragging files from one location to the other. I don't deal well with opening a folder with 600 files and folders and just start deleting them. Too cluttered. Also, it doesn't mean I want to delete those files, maybe I just want to have a "My Files" folder with just the relevant files and folders for the next year (which is the case here).

Now, this is just one example, not the only one. I find myself looking for that workflow here and there, so it would be something that could help me with other scenarios.

The importance of keeping the icons is because it just makes everything easier when I'm looking at my list of folders when I want to find something. I don't have to read their names.

As I mentioned on my original post, I was able to make it work using the Terminal as explained on that article I shared. The only issue is that I have no idea how to still use Terminal to include the custom icons...

I mean, if using AppleScript achieves the same thing AND can also include the icons, whatever makes it work, I'm good with it :slight_smile:

Have you read this? Looks like rsync might do it. If you scroll down, there's mention of including icons and labels.

It wouldn't matter if you did -- you also need to change the "hidden" x attribute entries of folder you want the icon applied to. That's why this is non-trivial.

You don't -- the macro I posted above does all that for you.

rsync is looking to be a possibility, but

...that is going to be really difficult, especially if you want to change "things to keep" on a whim. At that point you'd be much better off using the KM macro and instead of deleting "everything visible which isn't a folder" you could add you own conditions, like "everything visible that isn't a folder except files with My Files in their path".

No, it is perfectly true.

Reproducing a folder structure with AppleScript is not the same as copying it.

I'm not aware of a convenient means to test for and to copy a custom folder icon with Applescript, although it might be possible with Applescript Objective-C.

There is a 3rd party command line utility, although I haven't looked at it in years.