Skip to Content

AppleScript: Delete old folders

If you work like I do, then you have a hierarchy of folders contained in a single folder that allows you to manage project files, notes, etc. Well, when it comes time to chunk those old files, normally you have to manually go through and remove them; this task can become daunting if you have multiple project folders. I am going to show you an easier way. In this how-to, we'll cover how to create an AppleScript that will allow you to remove folders that are older than 30 days.

Continue reading to learn how to create this AppleScript.


Creating the AppleScript
Open Script Editor.app (it can be found in /Applications/AppleScript). When you have the application opened, type (or copy / paste) the entire AppleScript below.


set backupFolder to (choose folder)
tell application "Finder" to set theseFolders to (get folders of backupFolder)
repeat with oneFolder in theseFolders
if (creation date of (info for oneFolder as alias) < (current date) - 30 * days) then
tell application "Finder" to delete oneFolder
end if
end repeat


When you are finished, your script should look similar to the one below.

Saving the script
You can save this script as an application by clicking File > Save As, and choosing "Application" from the "File Format" drop-down box. Type a name and location for your script to be saved and then click the "Save" button.

Running the script
To run this script, you can double-click on the application that you just created or you can drag it into the dock for easy clicking. When you run the script, you will be presented with a dialog asking you for a folder. Browse to the parent (top-level) folder that contains folders you would like to be searched by date. The script will then find the folders that are older than 30 days and move them to the Mac OS X trash bin.

Additional notes
  • Before you remove the files from the trash bin, make sure they are the files you wanted to delete.
  • This script only removes folders, so be sure to organize your files within the folders accordingly.
  • Be cautious when using this script, as it moves files. TUAW is not responsible for data loss due to incorrect usage of this script.


Categories

Features How-tos

If you work like I do, then you have a hierarchy of folders contained in a single folder that allows you to manage project files, notes,...
 

Add a Comment

*0 / 3000 Character Maximum

10 Comments

Filter by:
Ian

Why not just highlight them all and hit command-delete?

May 06 2008 at 12:25 PM Report abuse rate up rate down Reply
Mason Cantar

And how would the script look for empty folders?

May 05 2008 at 3:37 PM Report abuse rate up rate down Reply
Murphy Mac

Picazsoo - I'll be the first to admit I often forget how many posters don't speak English as their first language. And I'm duly impressed that they can master technology in anything other than their native language.
But the "just" thing pervades every single posting about how to do something. And people who DO speak English as their first language use the phrasing over and over again. I don't know if they realize it's offensive or not. But it is. Simply adding the word "just" devalues the content of the post, if people realize it or not.
I apologize for offending you. I don't know why I ever mention the JUST thing, it's one of those things I need to learn to ignore if I'm going to participate in sites like this.



May 05 2008 at 2:58 PM Report abuse rate up rate down Reply
1 reply to Murphy Mac's comment
Picazsoo

I'll definitely try to avoid this evil "just" from now on (-:

About the tech stuff: Almost all technology I use is in english language. I went to Texas last year as an exchange student, but there are some things one will never learn unless one's a real native speaker.

I'm glad we figured this out.

End of Transmission

May 05 2008 at 3:24 PM Report abuse rate up rate down Reply
Murphy Mac

Some day I'd like to see a how-to post somewhere, anywhere, without any comments that say, "Wouldn't it be easier to just..."

Go ahead, suggest ANOTHER way. The more ways the better. But to suggest your way is the best way and every other way is inferior is arrogant. Take a look around. Just, just just just just just. "Why not just....?" "Couldn't you just.....?" So annoying.

Look - JUST write your other method without slamming the way someone JUST took the time to write up. Leave the JUST out.

May 05 2008 at 1:07 PM Report abuse rate up rate down Reply
2 replies to Murphy Mac's comment
Timmargh

Well said Murphy. Well said.

May 05 2008 at 2:33 PM Report abuse rate up rate down Reply
Picazsoo

I am sorry if my comment did sound like that. I am not from an english speaking country so these nuances in language aren't as easy to understand as you might think.

On the other side, I didn't want to be offensive in any way, but you surely wanted to. Actually the reason I posted what I posted was because I hoped someone would suggest the way to do what I wanted. I use Hazel to do this kind of stuff.

Sincerely, Jachym Metlicka

May 05 2008 at 2:33 PM Report abuse rate up rate down Reply
Bob

find /path/to/directory -type d -mtime +30 -exec rm -r {} ;

May 05 2008 at 12:31 PM Report abuse rate up rate down Reply
1 reply to Bob's comment
Picazsoo

Thanks. That's much easier (-:

May 05 2008 at 2:34 PM Report abuse rate up rate down Reply
Picazsoo

Wouldn`t it be safer to just label those folders with a certain colour and later search in Finder for all folders with this label and delete them from there? It is much safer than just moving them straight to trash...

Just my 2 cents...

May 05 2008 at 11:53 AM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.