Page 1 of 2
Delete oldest if more than 5 files in folder

Posted:
Sat Jun 18, 2011 3:32 am
by macmedix
Hi, I have an app that make a huge backup file nightly. (It takes a couple of hours to make that file.) I want to always keep 5 backups, but delete the oldest files if/when there are more than 5 files in this folder.
If we tell Hazel to delete files older than a certain date that could delete all the backups in the case of new backups not being generated; which would be bad.
So we need to delete only the files that are not the 5 more recent backups in this folder. What is a good strategy to accomplish this?
For extra credit: How could we keep 5 daily files and also keep 1 file per week so we have 5 more weekly files, while deleting the oldest excess backups? (sort of like TimeMachine).
THANKS!
DN
Re: Delete oldest if more than 5 files in folder

Posted:
Mon Jun 20, 2011 3:02 pm
by Mr_Noodle
You can't do this now without a script. If you check the beta forum, you'll see a feature like this is planned for 3.0. I suggest checking there to see if it matches what you expect.
Re: Delete oldest if more than 5 files in folder

Posted:
Mon Jun 20, 2011 4:24 pm
by macmedix
That new feature looks like it will be VERY helpful when it becomes available. Until then, Using a script would be ok, if I had one.
Can anyone show me an Applescript to delete all but the 5 most recently created items in a folder?
Given:
folder name is /Users/Shared/DayliteBackups/
new backups are expected to be produced daily and are about 15GB each.
we don't care about anything else in this folder, so ok to delete all except what we are trying to manage.
THANKS!
Re: Delete oldest if more than 5 files in folder

Posted:
Fri Mar 02, 2012 5:40 pm
by Gasport
I have just purchased Hazel 3 and wish to do something quite similar. I would like to keep the 10 most recent backup files and delete the oldest one as a new backup is created. How do I do this in Hazel 3?
Re: Delete oldest if more than 5 files in folder

Posted:
Fri Mar 02, 2012 5:51 pm
by macmedix
Hi, I could easily be wrong, but I don't think Hazel can do that. Hazel can delete files older than a certain number of days, which might seem to fit the bill. But that can go really wrong if your backups stop producing new files. Then Hazel keeps deleting backups on schedule until you have none left. So that is not a recommended way to approach this.
I resolved my issue with a fancy bit of command line and cron. But it is working ok.
What is producing the files you need to manage? Do they have similar file names? Is there anything else in that folder?
Dave Nathanson
Mac Medix
http://nathanson.org/davesays
Re: Delete oldest if more than 5 files in folder

Posted:
Sat Mar 03, 2012 10:54 am
by Gasport
[quote="macmedix"]Hi, I could easily be wrong, but I don't think Hazel can do that. Hazel can delete files older than a certain number of days, which might seem to fit the bill. But that can go really wrong if your backups stop producing new files. Then Hazel keeps deleting backups on schedule until you have none left. So that is not a recommended way to approach this.
I resolved my issue with a fancy bit of command line and cron. But it is working ok.
What is producing the files you need to manage? Do they have similar file names? Is there anything else in that folder?
I have several manual backups such as 1Password, Quicken, and License Keeper to mention a few. None of them appear to limit the number of backups you can create as I have seen in some apps. Right now the options are to keep adding to the number of backups or manually manage them, which is what I was trying to get Hazel to do for me. I then backup these to an external drive to be double sure I have the latest update of the data in these apps.
Re: Delete oldest if more than 5 files in folder

Posted:
Sun Mar 04, 2012 5:25 pm
by Mr_Noodle
You should be able to do this with Hazel now.
Something like:
- Code: Select all
If (all) are true for the (current file/folder)
If (all) are true for the (enclosing folder)
Number of items > 5
Date Added is among the 1 least recent
Then:
Move file to trash
I suggest playing with that.
Re: Delete oldest if more than 5 files in folder

Posted:
Sun Mar 04, 2012 9:08 pm
by Gasport
Thanks, will do and let you know if it works.
Re: Delete oldest if more than 5 files in folder

Posted:
Wed Jun 27, 2012 2:21 pm
by jasonbraganza
I need this too! Unfortunately I can’t seem to get it to run
I’ve set the rule to any file & pasted the script as is, as an embedded applescript
The rule refuses to set.
I get a Rule error
At least one of the actions has an empty field or an error. Please fill in the field, correct the error or remove the action.
Checking the rule says this at the bottom
Expected “then”, etc. but found identifier. with are highlighted on the first line... Am I missing something?
Re: Delete oldest if more than 5 files in folder

Posted:
Thu Jun 28, 2012 3:26 pm
by Mr_Noodle
There is no script involved. What was posted before is the actual rule. Just configure it as shown.
Re: Delete oldest if more than 5 files in folder

Posted:
Fri Jun 29, 2012 2:24 am
by jasonbraganza
Doh!

Will try and post back. Am a newbie to Hazel, so it’ll take a bit of experimenting.
Jason
Re: Delete oldest if more than 5 files in folder

Posted:
Fri Jun 29, 2012 6:18 am
by Gasport
Fear not, it is an easy procedure but just took me a bit to get used to setting up the rule. Works like a charm.
Re: Delete oldest if more than 5 files in folder

Posted:
Fri Jun 29, 2012 9:37 am
by jasonbraganza
Tada! Got it working.
set it up like shown here...
viewtopic.php?f=4&t=1435Thank you Mr. Noodle for making such a smashingly awesome program!
Two days with it and i’ve automated clean up of files, startup of torrents & conversion of ebooks!
How did I live without hazel before?
Re: Delete oldest if more than 5 files in folder

Posted:
Thu Apr 18, 2013 1:47 pm
by Gasport
Back again. Had this working great on my old iMac and moved to a new one. Tried to recreate the rule by importing it and copying for the now present Quicken folder. Hazel does identify the files in the folder but when I use the Number of items > 10, I get an error. There are more than 10 files in the folder
I have a screenprint of the error but cannot attach.
***-[_NSPlaceholderDictionary initWithObjects:forKeys count]: attempt to insert nil object from objects[0]
This is from the ! Indicates possible issue.......
It sounds to me like the count is not working and Hazel is seeing 0 files in the folder. Any idea what this means?
I did do an import of the rule from a backup I had. I am sure I am doing something wrong but cannot figure it out.
Re: Delete oldest if more than 5 files in folder

Posted:
Fri Apr 19, 2013 3:52 pm
by Mr_Noodle
Can you email support with the full stack dump?