Delete oldest if more than 5 files in folder

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

Moderator: Mr_Noodle

Delete oldest if more than 5 files in folder 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
macmedix
 
Posts: 7
Joined: Sat Jun 18, 2011 3:20 am

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

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!
macmedix
 
Posts: 7
Joined: Sat Jun 18, 2011 3:20 am

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?
Gasport
 
Posts: 14
Joined: Sun Mar 30, 2008 1:42 pm

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
macmedix
 
Posts: 7
Joined: Sat Jun 18, 2011 3:20 am

[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.
Gasport
 
Posts: 14
Joined: Sun Mar 30, 2008 1:42 pm

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

Thanks, will do and let you know if it works.
Gasport
 
Posts: 14
Joined: Sun Mar 30, 2008 1:42 pm

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?
jasonbraganza
 
Posts: 3
Joined: Wed Jun 27, 2012 2:14 pm

There is no script involved. What was posted before is the actual rule. Just configure it as shown.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Doh! :)

Will try and post back. Am a newbie to Hazel, so it’ll take a bit of experimenting.

Jason
jasonbraganza
 
Posts: 3
Joined: Wed Jun 27, 2012 2:14 pm

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.
Gasport
 
Posts: 14
Joined: Sun Mar 30, 2008 1:42 pm

Tada! Got it working.

set it up like shown here...
viewtopic.php?f=4&t=1435

Thank 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?
jasonbraganza
 
Posts: 3
Joined: Wed Jun 27, 2012 2:14 pm

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.
Gasport
 
Posts: 14
Joined: Sun Mar 30, 2008 1:42 pm

Can you email support with the full stack dump?
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Next

Return to Support

cron