Page 1 of 1

delete older files when a folder excess a certain size

PostPosted: Mon May 02, 2011 11:34 pm
by Vlad Ghitulescu
Hello!


I'm new to Hazel and hope to find an answer to my problem: I would like to monitor the size of a backup-folder and delete the older backup-files when the size of the backup-folder exceeds a certain value.

Could you please give me some advice how to do this in Hazel?

Thanks!


Regards,
Vlad Ghitulescu

Re: delete older files when a folder excess a certain size

PostPosted: Tue May 03, 2011 10:00 am
by Mr_Noodle
While you can filter on folder size, there's no way to pick out the oldest files. That said, look in the beta forum as you may see something of interest there.

For the folder size part, you could do something like:

If Kind is Folder and Size > ??? , then Run rules on folder contents

That would make Hazel go into any oversized folders. The second rule needed can't be done now but keep the above in mind for future reference.

Re: delete older files when a folder excess a certain size

PostPosted: Tue May 03, 2011 5:30 pm
by Vlad Ghitulescu
Hello, Mr._Noodle!


Thanks for your reply!

The solution I can imagine sound something like:

---
loop until folder_size <= desired_size
delete only the oldest file in the folder
end-loop
---

The loop...end-loop should be your "If Kind is Folder..." I suppose, but what corresponds to the delete-line?

Perhaps is something to be done as a script?

Thanks again!


Regards,
Vlad Ghitulescu

Re: delete older files when a folder excess a certain size

PostPosted: Wed May 04, 2011 2:26 pm
by Mr_Noodle
Hmmm, interesting point about the looping. The problem now is that once a rule successfully runs against a particular file, if that file keeps matching the rule, it won't get executed. I'd need to think about how to get around that. Off the top of my head now, maybe do something like "size < ?? and date last matched is not in the last minute". That would only make it loop every minute but it's the best I can think of at the moment short of a script.

no loop?

PostPosted: Wed May 04, 2011 6:12 pm
by Vlad Ghitulescu
Hello, Mr_Noodle!


Thanks again for your reply!

That means that there's no loop? :-(

How do these other rules (as "move every png-file from Desktop") are applied periodically? I thought that there's a similar concept behind:

---
focus on folder "Desktop"
loop until "no-png-file-anymore"
delete first jpg-file
end-loop
---

Was this wrong?


Regards,
Vlad Ghitulescu


P.S. Perhaps it's time for me to finally learn Apple Script! :-) Do you have an advice, where to start? Or is there anything else more usable than Apple Script for my problem?

Re: delete older files when a folder excess a certain size

PostPosted: Thu May 05, 2011 1:37 pm
by Mr_Noodle
It's not so much that there is no loop. It's that the condition doesn't change and Hazel is set up to only run the rule the first time. The logic goes basically like this:

First time through:
- Rule A matches file X. Execute A's actions on X.

Second time through:
- Rule A still matches file X. Do not execute actions again on X.

Right now, the only way for it to work is if you structure the conditions so that the file doesn't match any rules temporarily (or matches a different rule) then matches rule A again, at which point, the actions will get executed.

I'll take this situation into consideration but adding some built-in looping construct can be dangerous as users are liable to create infinite loops.

As for AppleScript, you can google around or pick up a book. I believe this is the one most people recommend: AppleScript: The Definitive Guide

Re: delete older files when a folder excess a certain size

PostPosted: Thu May 05, 2011 3:55 pm
by Vlad Ghitulescu
Hello again!


I thought that a rule is repeatedly proven until it is not valid anymore: if at 10 a.m. the trash-size exceeds 1 GB (= matching the rule A), the trash will be emptied. If I trash so much after this that the trash exceeds 1 GB let's say at 10.30 a.m. (= the rule A is matched again), so the trash will be emptied again. If not, the rule is not matched and nothing happened.
So I believed that something like a cron-job (aka a loop :-) proves again and again all of my Hazel-rules and applies them only when matched.

If wrong, what makes a rule like above be checked again (at 10.30 a.m.) if already matched at 10 a.m.?


Slightly confused, :-)
Vlad Ghitulescu

Re: delete older files when a folder excess a certain size

PostPosted: Fri May 06, 2011 3:50 pm
by Mr_Noodle
The Trash settings use a different mechanism than the rule engine.

Basically, the rule engine executes actions when the conditions change such that the rule becomes true. It won't fire them again if the conditions continue to be true (unless it failed to execute before). If it changes to match another rule or no rule at all then changes back, then the actions would be executed again in that case.