Sort files into subfolder based on part of a name.

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

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?
dekaritae
 
Posts: 8
Joined: Sat May 05, 2012 12:18 am

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].
ecormany
 
Posts: 28
Joined: Wed Apr 25, 2012 6:15 pm

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.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

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.
maeltj@me.com
 
Posts: 3
Joined: Fri Jan 28, 2011 5:06 am

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.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

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.
maeltj@me.com
 
Posts: 3
Joined: Fri Jan 28, 2011 5:06 am

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.
Mr_Noodle
Site Admin
 
Posts: 11868
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

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]
dekaritae
 
Posts: 8
Joined: Sat May 05, 2012 12:18 am

My mistake, thought that was a snippet of AppleScript. This is what happens when you give up coffee.
dekaritae
 
Posts: 8
Joined: Sat May 05, 2012 12:18 am

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?
dekaritae
 
Posts: 8
Joined: Sat May 05, 2012 12:18 am

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?
Mr_Noodle
Site Admin
 
Posts: 11868
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

There would be a second rule, similar to the first, which matches based on Comment, and sorts into folder based on that.
dekaritae
 
Posts: 8
Joined: Sat May 05, 2012 12:18 am

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.
Mr_Noodle
Site Admin
 
Posts: 11868
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

That works perfectly, though I swapped the order as I think I'd prefer it to match the Comment even if the Name matches.
dekaritae
 
Posts: 8
Joined: Sat May 05, 2012 12:18 am


Return to Support

cron