Flatten folder hierarchy

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Flatten folder hierarchy Sun May 27, 2012 8:05 am • by stevenringo
Hi,

Any idea how I could flatten a folder hierarchy?

I would like to take all image files that are in various subfolders and merge them into one folder.

I have tried moving files, but it seems to move the files and their respective parent folders.

Code: Select all
├── 010f327efe027a72764957ffc736de931ccf3f74ed
│   └── IMG_1121.JPG
├── 0195a7b9eb36c05e919395eacabc561551ff084c95
│   └── IMG_1120.JPG
├── 01b63d1b9a78d3dab18580863000243915dfda4555
│   └── IMG_0302.JPG
└── 01c1afc6cf24b9ec33cc6c265b670a39de6af0d0a8
    └── IMG_0290.JPG


In this example, I would like a folder that contains just the 4 files. I can then sort these as usual.

(Aside: This folder structure actually comes from PhotoStream)
stevenringo
 
Posts: 2
Joined: Sun May 27, 2012 7:54 am

Re: Flatten folder hierarchy Sun May 27, 2012 9:25 am • by stevenringo
OK, I worked it out.

The confusing thing is the difference between (a) recursively walking folders using the infamous sticky post that gets referred to in many posts and (b) the options titled "any/all of its subfolders"

Perhaps Mr Noodle could explain the difference between them, and the use cases for each respectively.

Thanks!
stevenringo
 
Posts: 2
Joined: Sun May 27, 2012 7:54 am

Re: Flatten folder hierarchy Sun May 27, 2012 11:47 pm • by ecormany
doing this requires the appropriate combination of very simple rules. at minimum you need two parts: a rule that drills down the file hierarchy and a rule that moves image files. you can add an optional rule that cleans up empty subfolders.

the order of the rules is important! it has to be:
1. delete empty folders
2. traverse (non-empty) folders
3. move images

here are how those rules can look.

Delete Empty Folders
Code: Select all
if
     Kind is Folder
     Sub-file/folder Count is 0
then
     Move to Trash


Traverse Folders
Code: Select all
if
     Kind is Folder
then
     Run rules on folder contents
if a folder is empty, the earlier rule will take precedence and trash it. if it has contents, they will be traversed.

Move Image Files (fixed location)
Code: Select all
if
    Kind is Image
then
    Move to [top level]


Move Image Files (push upwards)
Code: Select all
if
    Kind is Image
then
    Move to enclosing folder


i prefer the second formulation of the Move rule. if you have a deeply nested folder structure, it takes multiple steps to push the files all the way to the top (but Hazel will take care of that!). the benefit of using it is if you want to re-use this set of rules, you can just drag and drop them onto the new folder in Hazel. if you used a fixed location, then you'd have to edit the rule and manually specify the new destination.

if you have several folders you want to flatten at once, or want to create a place where you can drop a folder and it will auto-flatten, you can even get fancier by using subfolder depth! use the same set of rules on the folder containing the ones to be flattened, changing the Move rule to the following.

Move Image Files (push upwards)
Code: Select all
if
    Kind is Image
    Subfolder depth is greater than 1
then
    Move to enclosing folder
ecormany
 
Posts: 28
Joined: Wed Apr 25, 2012 6:15 pm

Re: Flatten folder hierarchy Tue May 29, 2012 3:05 pm • by Mr_Noodle
The help has an explanation but if you use the "sub-files/folders" target, you are not matching those files. Read the wording in the current version carefully. You are matching a folder whose subfiles/folders meet those conditions, not those subfiles themselves. It's like saying, this rule matches if this folder has any subfiles that are purple. Note, this rule matches if the file is purple. See the difference?
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Flatten folder hierarchy Wed Oct 17, 2012 1:47 pm • by alips
stevenringo wrote:OK, I worked it out.


Could you post your recipe, please?
alips
 
Posts: 1
Joined: Wed Oct 17, 2012 1:21 pm

Re: Flatten folder hierarchy Tue Aug 04, 2015 11:32 pm • by reagan9000
I'm not the original poster, but maybe this will help someone else. Here's my recipe for creating a flat backup of my iCloud Photostream folder hierarchy:

1) Find Photostream Folder:
If Kind is Folder then
Run rules on folder contents

2) Copy Photostream Image:
If Kind is Image and
Subfolder Depth is greater than 0
Copy to folder (my backup folder) with options if file exists do not copy the file and do not copy if duplicate

I didn't realize that one could create multiple rules that worked together - I thought all the testing logic and actions had to be in a single rule. Now I know.
reagan9000
 
Posts: 6
Joined: Sat Jun 13, 2015 3:28 am


Return to Open Discussion