I need help with a complicated rule

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

Moderator: Mr_Noodle

I need help with a complicated rule Tue Aug 14, 2018 10:47 pm • by Sonnenklee
Hello everybody,

I'm currently using the trial version of Hazel and I'm very excited. I will definitely buy it.

But now I have a question about a very complex rule that I can't handle.

A zip file is saved in a folder "Downloads". This file should be unzipped. The process creates a subfolder named "Data_ <month> _ <year>". The file "Rg_XXXXXXXXXX.pdf" is created in this subfolder.

Hazel should rename this file in the subfolder to "RE <month-1>. <Year> .pdf". Month and year can be taken either from the name of the subfolder or the creation date of the PDF file.

Because that's not complicated enough: <month-1> can yield 0. If this happens, then <month> must be 12 and <year-1>.

Finally, the renamed file should then be moved to another folder and the subfolder "Data_<month>_<year>" containing the unpacked file should be deleted.

Is that possible with Hazel? If so, I ask for help how that works. All my attempts lead to no useful result. At the moment I just managed to unzip the zip file.
Sonnenklee
 
Posts: 5
Joined: Tue Aug 14, 2018 10:24 pm

Re: I need help with a complicated rule Wed Aug 15, 2018 10:32 am • by Mr_Noodle
First off, Hazel does not go into subfolders by default. You need to create a separate rule to tell it to do so. Search the help for "subfolders" as there is a chapter on that.

Also look up "match patterns". That should allow you to pick off pieces of the name and re-arrange as necessary.

The issue with detecting month 0 is a bit trickier. I suggest getting the regular stuff working first before focusing on this. Now, you may need to create a duplicate rule which parses the date. The month would be captured in a custom attribute. You can then use that custom attribute in a condition. You can check for whether it is 0 and then use a number adjustment on the year in the actions.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: I need help with a complicated rule Wed Aug 15, 2018 9:17 pm • by Sonnenklee
Thank you for your answer Mr_Noodle.

I had already read "match patterns" in advance. Unfortunately, that doesn't help me. I have to read out the name of the subfolder and use this name modified as filename for the PDF file.

Reading out the name of the subfolder works. If I then want to rename the PDF file, the subfolder is always renamed but never the PDF file.

How should that be realized? If I try it over the creation date of the PDF file then "matches" is not available.

I have inserted the rule for subfolders.
Sonnenklee
 
Posts: 5
Joined: Tue Aug 14, 2018 10:24 pm

Re: I need help with a complicated rule Thu Aug 16, 2018 10:56 am • by Mr_Noodle
Can you post your rules? You'll need to use an external image hosting site (like imgur) to post any images.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: I need help with a complicated rule Thu Aug 16, 2018 11:57 am • by Sonnenklee
Here is an example of a rule. I've already tried a few rules, but none works properly. This one renames the subfolder, how the file should actually be renamed.

Image
Sonnenklee
 
Posts: 5
Joined: Tue Aug 14, 2018 10:24 pm

Re: I need help with a complicated rule Fri Aug 17, 2018 8:56 am • by Mr_Noodle
Ok, this rule won't match anything as it is trying to match both a file and a folder at the same time.

Now, if you want to match a file and use the name of the folder containing it, do something like:
Code: Select all
    ...
    If (all) are met for (the enclosing folder)
        Name matches ...


That way you still match the file while having custom attributes grabbing values from the parent folder's name.
Mr_Noodle
Site Admin
 
Posts: 11251
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: I need help with a complicated rule Fri Aug 17, 2018 12:44 pm • by Sonnenklee
That was the deciding hint. Thank you for the great help. Now it works (nearly perfect).

I have created 5 rules:

1. For unzipping
2. Rename file
3. If month isn't 00 then move file
4. If month is 00 then rename file to "RE 12.<year -1>" and move file
5. Delete directory

But there is only one small flaw left. If the folder is named "Data_10_<year>" the file should be renamed to "RE 09.<year>". But the file is renamed in "RE 9.<year>". When calculating <month -1>, the format "00" does not seem to work if the origin was two digits and the result is single-digit. Is this a bug or intended?

Must i create a sixth rule for this case or is there an easier way? Sorry for so many questions from me and thanks again for the help.
Sonnenklee
 
Posts: 5
Joined: Tue Aug 14, 2018 10:24 pm

Re: I need help with a complicated rule Sun Aug 19, 2018 1:36 pm • by Sonnenklee
I could answer my last question by myself.

I created "month" in the pattern with digits. If I use number instead of digits then format "00" works. Mostly it depends on the subtleties.
Sonnenklee
 
Posts: 5
Joined: Tue Aug 14, 2018 10:24 pm


Return to Support