Page 1 of 1
Sort files into subfolder based on part of a name.

Posted:
Sat May 05, 2012 12:22 am
by dekaritae
I'd like to use Hazel to sort various files from members of DeviantART into subfolders based on their username. Files from DA are saved with the name format "Title_by_Username-######.jpg", the hashes being an alphanumeric string. How would you suggest I script this in Hazel?
Re: Sort files into subfolder based on part of a name.

Posted:
Sun May 06, 2012 8:37 pm
by ecormany
if Username is always an alphanumeric string (no spaces or punctuation), this should work:
- Code: Select all
if
Name matches […]_by_[Username]-[123].jpg
do
Sort into subfolder with pattern [Username]
make Username a custom token that matches Letters & Digits [12ab].
Re: Sort files into subfolder based on part of a name.

Posted:
Tue May 15, 2012 2:34 pm
by a_freyer
dekaritae wrote:I'd like to use Hazel to sort various files from members of DeviantART into subfolders based on their username. Files from DA are saved with the name format "Title_by_Username-######.jpg", the hashes being an alphanumeric string. How would you suggest I script this in Hazel?
@ecormany's solution is what'd I'd do, except since the hashes are alphanumeric:
- Code: Select all
if
Name matches […]_by_[Username]-[12ab]
do
Sort into subfolder with pattern [Username]
Friendly Reminder: Keep support requests in the Support Forum.
Re: Sort files into subfolder based on part of a name.

Posted:
Tue Jul 17, 2012 1:53 pm
by maeltj@me.com
Hi,
I'm trying to do this but I cant.
I want to name files like this:
yyyy-mm-dd - [Tag] - [Name]
Ex: 2012-07-17 - Receipt - Nike FuelBand.pdf
I create rules to move the file to a folder starting with R, but I want to create a rule to move/create if not exists a folder within the folder R.
If this possible?
I got a lot to learn about script

Thanks.
Re: Sort files into subfolder based on part of a name.

Posted:
Tue Jul 17, 2012 2:03 pm
by a_freyer
Is the folder name going to be "R" or is it going to be a word that starts with "R"?
We're going to need a bit more information, but generally speaking Hazel will create folders for you if you follow the suggestions above.
Look into "Sort into Subfolder" on the forums.
Friendly Reminder: Keep support requests in the Support Forum.
Re: Sort files into subfolder based on part of a name.

Posted:
Tue Jul 17, 2012 2:48 pm
by maeltj@me.com
Thanks,
Now I have a folder called "R"
I make a rule to add he name of the file as a comment. Then drag this file in to a folder to process, it adds the date and then move the file to the archive folder. This folder contents folders from A to Z.
Using a rule based on the comment hazel moves the file to the folder "R".
As in my example, I use the first word of the name, as a Tag. What I want to do is create a Folder for each tag(if not exists) and put the file in.
Is it Possible?
Thanks, now looking into "Sort into Subfolder" on the forums.
Thanks again.
Re: Sort files into subfolder based on part of a name.

Posted:
Tue Jul 17, 2012 3:42 pm
by Mr_Noodle
If you want to sort into subfolder based on the first latter, then create a match pattern with a custom token to match the first letter, then use that custom token in the sort into subfolder action. See the examples in the help for "custom token" as well as the others above which have been helpfully posted by others.
Re: Sort files into subfolder based on part of a name.

Posted:
Thu Jun 04, 2015 2:53 am
by dekaritae
Using this script gives me the error "Expected expression but found end of line."
if
Name matches […]_by_[Username]-[12ab]
do
Sort into subfolder with pattern [Username]
Re: Sort files into subfolder based on part of a name.

Posted:
Thu Jun 04, 2015 3:07 am
by dekaritae
My mistake, thought that was a snippet of AppleScript. This is what happens when you give up coffee.
Re: Sort files into subfolder based on part of a name.

Posted:
Thu Jun 04, 2015 3:22 am
by dekaritae
That works quite well.
I'm wondering now, how I would modify the script so that it doesn't create new folders, only move files into folders that were already existing?
Re: Sort files into subfolder based on part of a name.

Posted:
Thu Jun 04, 2015 11:10 am
by Mr_Noodle
Only way to do that is to have the pattern not have the pattern match non-existent folders. My question is: how would you want it to behave in those cases, if it doesn't create the folder?
Re: Sort files into subfolder based on part of a name.

Posted:
Thu Jun 04, 2015 12:37 pm
by dekaritae
There would be a second rule, similar to the first, which matches based on Comment, and sorts into folder based on that.
Re: Sort files into subfolder based on part of a name.

Posted:
Thu Jun 04, 2015 2:24 pm
by Mr_Noodle
You can combine them into one rule. Have a condition match on name, using a custom attribute to match the portion you want to reuse in the folder name. Then have another condition to match the comment, re-using the same custom attribute. Something like:
- Code: Select all
If (any) are met
Name matches <blah blah>(• your custom attribute)<blah blah>
Comments match <blah blah>(• your custom attribute)<blah blah>
If the name matches, you use the value captured in the custom attribute. If the name doesn't match, since it is set to "any", the next condition is checked. If it matches there, the custom attribute will have the appropriate value. If neither matches, then the rule doesn't match and the actions are executed.
Re: Sort files into subfolder based on part of a name.

Posted:
Thu Jun 04, 2015 6:27 pm
by dekaritae
That works perfectly, though I swapped the order as I think I'd prefer it to match the Comment even if the Name matches.