How to get Hazel to go into subfolders

[NOTE: this post is superceded by this page in the user guide: Processing Subfolders
This question comes up a lot so I thought I'd answer it here and set it as sticky so that it's in a prominent place. Please make sure to read the whole thing before posting any questions about subfolders. Of course, if you have additional questions or suggestions, please post them.
Can Hazel go into subfolders?
Yes.
How?
In the simplest case, just create a rule like the following:
How this works is when Hazel encounters a folder, it will go into it and run its rules on the files inside as it was doing with the top level folder. The "Run rules on folder contents" action only applies to folders and tells Hazel that it should descend into this folder.
For example, if you have a folder with subfolders A, B and C and you have rules 1, 2, 3 and 4 in your rule set. Let's say rule 1 is the rule described above. When Hazel hits folder A, it will go inside it and then try and apply rules 1, 2, 3, 4 on all the files and folders inside it. That also means that for any folders inside A that it finds, Hazel will descend into them as well (as dictated by rule 1). When it pops back out, it will do the same for subfolders B and C as it gets to them.
Now how does it not work?
Remember the above is not magic in that it is a rule just like any other. The ordering matters. If you have a rule matching folders before this one, this one will not be run. Also, you can't mix file types into the same rule. A common mistake is:
Here, the logic for two different rules are being combined into one rule. Chances are, a Music file cannot be a folder at the same time. Also, the "Run rules on folder contents" only makes sense when run against folders so won't make sense when run against a file. Lastly, you probably don't want your folders moved into the "Music" folder.
The solution is to split them up into separate rules.
What if I only want Hazel to apply rules at the top level/level below the top level/etc.?
One thing Hazel does when descending is keep track of how deep it is in the folder tree. This is the "Subfolder depth". Starting at the top level, the subfolder depth is 0. Subfolders from there have a depth of 1 and so on. You can apply it to the folder rule above to limit how far down Hazel goes or you can use it in your rules to process your files, in which case, Hazel will still go as deep as it wants but that rule will only apply to files at certain levels that you specify.
Note: There is a bug in version 2.2.4 and earlier where the preview won't accurately show the effects of filtering on subfolder level. This does not affect how the rules will actually execute. This has been fixed in 2.3.
Can I download this rule?
You sure can. First off, if you are not quite sure how this all works, I advise you do the following:
People tend to be surprised when using this for the first time, hence the above procedure.
You can download the rule here:
After downloading, double-click the file or drag it into your rule list.
Enjoy.
This question comes up a lot so I thought I'd answer it here and set it as sticky so that it's in a prominent place. Please make sure to read the whole thing before posting any questions about subfolders. Of course, if you have additional questions or suggestions, please post them.
Can Hazel go into subfolders?
Yes.
How?
In the simplest case, just create a rule like the following:
How this works is when Hazel encounters a folder, it will go into it and run its rules on the files inside as it was doing with the top level folder. The "Run rules on folder contents" action only applies to folders and tells Hazel that it should descend into this folder.
For example, if you have a folder with subfolders A, B and C and you have rules 1, 2, 3 and 4 in your rule set. Let's say rule 1 is the rule described above. When Hazel hits folder A, it will go inside it and then try and apply rules 1, 2, 3, 4 on all the files and folders inside it. That also means that for any folders inside A that it finds, Hazel will descend into them as well (as dictated by rule 1). When it pops back out, it will do the same for subfolders B and C as it gets to them.
Now how does it not work?
Remember the above is not magic in that it is a rule just like any other. The ordering matters. If you have a rule matching folders before this one, this one will not be run. Also, you can't mix file types into the same rule. A common mistake is:
Here, the logic for two different rules are being combined into one rule. Chances are, a Music file cannot be a folder at the same time. Also, the "Run rules on folder contents" only makes sense when run against folders so won't make sense when run against a file. Lastly, you probably don't want your folders moved into the "Music" folder.
The solution is to split them up into separate rules.
What if I only want Hazel to apply rules at the top level/level below the top level/etc.?
One thing Hazel does when descending is keep track of how deep it is in the folder tree. This is the "Subfolder depth". Starting at the top level, the subfolder depth is 0. Subfolders from there have a depth of 1 and so on. You can apply it to the folder rule above to limit how far down Hazel goes or you can use it in your rules to process your files, in which case, Hazel will still go as deep as it wants but that rule will only apply to files at certain levels that you specify.
Note: There is a bug in version 2.2.4 and earlier where the preview won't accurately show the effects of filtering on subfolder level. This does not affect how the rules will actually execute. This has been fixed in 2.3.
Can I download this rule?
You sure can. First off, if you are not quite sure how this all works, I advise you do the following:
- Stop Hazel (either via the menubar or the button in the "Info" tab)
- Set up your rules.
- Preview them (the preview item is in the cog/gear pull-down menu)
- When you are satisfied with the preview, start Hazel again.
People tend to be surprised when using this for the first time, hence the above procedure.
You can download the rule here:
After downloading, double-click the file or drag it into your rule list.
Enjoy.