Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacintoshMan

macrumors regular
Original poster
Feb 16, 2006
158
20
Hello all,

I'm attempting to script the finder into deleting every file beneath a folder that is older than a certain number of days old. Where I'm running into a problem is getting apple script to delete the files within the child folders as well.

I thought I got pretty close but when I use the "entire contents" container (which also deletes child files/folders) it wont allow me to specify age limit.

Here's what I got:

-- Set the age of files that you want to purge from your downloads folder
set modDate to (15)

-- Get current user's name
tell application "System Events"
set currentUser to (name of current user)
end tell

-- Check folder and move files to the trash that are older than N days.
tell application "Finder"
try
delete (entire contents in folder "tester" of folder "Desktop" of folder currentUser of folder "Users" of startup disk whose modification date is less than ((current date)) - modDate * days)
end try

end tell

tell application "Finder"
empty trash
end tell
 

chown33

Moderator
Staff member
Aug 9, 2009
10,753
8,433
A sea of green
You could do this more easily with an Automator workflow.

First step of the workflow is a "Filter Finder Items". Set it to use Date last modified, and a suitable range (e.g. is not in the last 15 days).

Second step is "Move Finder Items to Trash".

You could postpone adding the second step until after you're sure the first step is correct. That is, look at the output, make sure it's what you want, only then move them to trash.
 

MacintoshMan

macrumors regular
Original poster
Feb 16, 2006
158
20
You could do this more easily with an Automator workflow.

First step of the workflow is a "Filter Finder Items". Set it to use Date last modified, and a suitable range (e.g. is not in the last 15 days).

Second step is "Move Finder Items to Trash".

You could postpone adding the second step until after you're sure the first step is correct. That is, look at the output, make sure it's what you want, only then move them to trash.

Thanks for the suggestion.

For some strange reason when I set the 'Filter Finder Items' to those last modified in the last 15 days it misses some of the text files that were created/modified today (that i created as test files to see if it would pick them up)
 

chown33

Moderator
Staff member
Aug 9, 2009
10,753
8,433
A sea of green
1. Make a workflow with "Get Specified Finder Items"; add your user's Desktop/tester folder to its list (the "Add..." button). Follow it with a "Get Folder Contents" with "Repeat for each subfolder found" enabled.

Confirm that the output contains your test files. Post that output as text, and identify the exact files that were created/modified today.

The above should show every file, regardless of date. If it doesn't show your test files, then they aren't where you think they are.


2. Copy and paste this exact command-line into a Terminal window:
Code:
find ~/Desktop/tester -mtime 1 -ls
Then copy and paste the complete output into a post here. Use CODE tags on the pasted output, so it retains its formatting.

Again, identify exactly which files are the recently modified test files.

The criteria for being output by 'find' is "modified within the last 1 day", so if your test files don't appear, then my first guess is the modified-date isn't what you think it is.

Please describe exactly how you created these files and changed their modification-time, and how you verified that their modification-time was what you expect it to be. In short, post your evidence (e.g. screenshot or 'ls' output) that confirms your expectation.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Hello all,

I'm attempting to script the finder into deleting every file beneath a folder that is older than a certain number of days old. Where I'm running into a problem is getting apple script to delete the files within the child folders as well.

Try this :

Code:
-- Check folder and move files to the trash that are older than N days.
tell application "Finder"
	try
		delete ([COLOR="Red"]files[/COLOR] of entire contents of folder "tester" of folder "Desktop" of folder currentUser of folder "Users" of startup disk whose modification date is less than ((current date)) - modDate * days)
	on error theErr number theErrNumber
		display dialog theErr & return & "The error number: " & theErrNumber
	end try
end tell
 

MacintoshMan

macrumors regular
Original poster
Feb 16, 2006
158
20
1. Make a workflow with "Get Specified Finder Items"; add your user's Desktop/tester folder to its list (the "Add..." button). Follow it with a "Get Folder Contents" with "Repeat for each subfolder found" enabled.

Confirm that the output contains your test files. Post that output as text, and identify the exact files that were created/modified today.

The above should show every file, regardless of date. If it doesn't show your test files, then they aren't where you think they are.


2. Copy and paste this exact command-line into a Terminal window:
Code:
find ~/Desktop/tester -mtime 1 -ls
Then copy and paste the complete output into a post here. Use CODE tags on the pasted output, so it retains its formatting.

Again, identify exactly which files are the recently modified test files.

The criteria for being output by 'find' is "modified within the last 1 day", so if your test files don't appear, then my first guess is the modified-date isn't what you think it is.

Please describe exactly how you created these files and changed their modification-time, and how you verified that their modification-time was what you expect it to be. In short, post your evidence (e.g. screenshot or 'ls' output) that confirms your expectation.

Sorry for the late response guys. I didn't want to reply unless it was verbose and low context. At any rate, the problem is that the automator filter for files created or modified within the last 15 days is inaccurate.

Hardware: iMac 3.2 i5 running OS X 10.8.5

As mentioned, I’ve set up a tester folder on my desktop to illustrate the problem. Inside the folder there is a text document I created through OS X’s TextEdit.app over two weeks ago, a zip folder I made today with some screenshots inside, a pdf that was modified over two weeks ago, two screen shots (one from today and the other from over two weeks ago, and lastly a folder that has a screen shot from today along with an assortment of files that are over a month old.

Here's a screenshot of the "tester" folder and its contents
RYIe9xK.png

Here's an image of whats inside /tester/untitled
aeDncBd.png



Here’s my Automator script:

I15HtX9.png


As you can see, after running the workflow, the “Get Folder Contents” function reveals all the files in the folder properly. Now, once we get to results of the “Filter Finder Items” function with the “Date Created” and “Date Modified” set to “in the last 14 days” we see that it turned up nothing. It should show the files I made today which are the zip, and a couple of screenshots.

Now lets change “Filter Finder Items” to find files where they were NOT created or modified in the last 14 days.

onXU0UB.png


As you can see, it picked up the zip file created today, along with a screenshot i created today that was located within the /tester/untitled fodler subfolder.

I ran the find ~/Desktop/tester -mtime 1 -ls which turned up nothing. Kind of loss because this seems like such a straight forward atimator script.

----------

Try this :

Code:
-- Check folder and move files to the trash that are older than N days.
tell application "Finder"
	try
		delete ([COLOR="Red"]files[/COLOR] of entire contents of folder "tester" of folder "Desktop" of folder currentUser of folder "Users" of startup disk whose modification date is less than ((current date)) - modDate * days)
	on error theErr number theErrNumber
		display dialog theErr & return & "The error number: " & theErrNumber
	end try
end tell

This fixed the script =] Thank you.

My final script ended up looking like this.

Code:
-- Set the age of files that you want to purge from your folder
set modDate to (15)

-- Get current user's name
tell application "System Events"
	set currentUser to (name of current user)
end tell

-- Check folder and move files to the trash that are older than N days.
tell application "Finder"
	try
		delete (files of entire contents of folder "tester" of folder "Desktop" of folder currentUser of folder "Users" of startup disk whose modification date is less than ((current date)) - modDate * days)
	on error theErr number theErrNumber
		display dialog theErr & return & "The error number: " & theErrNumber
	end try
end tell
 

MacintoshMan

macrumors regular
Original poster
Feb 16, 2006
158
20
So the applescript worked on small scale but when attempting to run it on one of the bigger FTP Volumes it would crash the finder.

I created a terminal command that did what I want:

Code:
find /path/to/files/ -type f -mtime +7 -exec rm -rf {} \;

I wanted to create an apple script application for it so I did:

Code:
tell application "Terminal"
	activate
	do shell script "find /path/to/files -type f -mtime +7 -exec rm -rf {} \;" 
end tell

but I ended up getting this:
y3FmeOL.png


Any ideas?

----------

I figured it out. Aparently if you want to use \ character in applescript you have to use two \\
 

lunaoso

macrumors 65816
Sep 22, 2012
1,332
54
Boston, MA
So the applescript worked on small scale but when attempting to run it on one of the bigger FTP Volumes it would crash the finder.

I created a terminal command that did what I want:

Code:
find /path/to/files/ -type f -mtime +7 -exec rm -rf {} \;

I wanted to create an apple script application for it so I did:

Code:
tell application "Terminal"
	activate
	do shell script "find /path/to/files -type f -mtime +7 -exec rm -rf {} \;" 
end tell

but I ended up getting this:
Image

Any ideas?

----------

I figured it out. Aparently if you want to use \ character in applescript you have to use two \\

If you also want to streamline the AppleScript even more, you can get rid of everything except the do shell script line. You don't need to do a tell terminal command to execute a shell script in AppleScript. So it could just be:
Code:
do shell script "find /path/to/files -type f -mtime +7 -exec rm -rf {} \\;"

Doesn't really shorten this particular script out a ton, but it might be good to know for future reference. :)
 
  • Like
Reactions: MacDann
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.