How can I make an applescript that search for files or folders older than x days (for example 7) and move them to my trashcan and delete them?
Can anywone help me?
How can I make an applescript that search for files or folders older than x days (for example 7) and move them to my trashcan and delete them?
Can anywone help me?
The following script deletes all files from a folder hierarchy that were last modified before 00:00 this day last week. (If they’ve never been modified, their modification date will be the same as their creation date.)
set cutoffDate to (current date) - 7 * days -- or 'weeks'
set time of cutoffDate to 0 -- 00:00 this day last week
set theFolder to choose folder
deleteOldFiles(theFolder, cutoffDate)
on deleteOldFiles(theFolder, cutoffDate)
tell application "Finder"
delete (every file of theFolder whose modification date comes before cutoffDate)
-- Recurse through the folder's subfolders
set theSubfolders to every folder of theFolder
repeat with thisSubfolder in theSubfolders
my deleteOldFiles(thisSubfolder, cutoffDate)
end repeat
end tell
end deleteOldFiles
This works in Mac OS 9.2.2. Unfortunately, until recently, the Finder’s ‘whose’ filter had a horrible bug whereby it couldn’t handle numerical comparisons, such as dates. (Unfortunately again, I’m not sure exactly when it was fixed.) If you have one of these older systems, you’ll have to loop through the files individually to check their modification dates. However, you can reduce the amount of work involved by using the Finder’s ‘sort’ command to return a list of the files arranged by date. You only have to loop through that until you find the point where the older files meet the younger or same-aged ones. thereafter you know that everything to one side is older and you can delete it en masse. The loop below is optimised on the assumption that you’ll run the script fairly regularly, so there will normally be fewer files to delete than to leave.
set cutoffDate to (current date) - 7 * days
set time of cutoffDate to 0 -- 00:00 this day last week
set theFolder to choose folder
deleteOldFiles(theFolder, cutoffDate)
on deleteOldFiles(theFolder, cutoffDate)
tell application "Finder"
-- Get a list of the files in this folder, arranged oldest to youngest
set theFiles to reverse of (sort every file of theFolder by modification date)
-- Search through the list for the first file that is *not* older than the cutoff date
set fileCount to (count theFiles)
repeat with i from 1 to fileCount
if modification date of (item i of theFiles) does not come before cutoffDate then
-- If a younger or same-aged file is found, exit the loop with the previous index
set i to i - 1
exit repeat
end if
end repeat
-- Delete all the older files - if any - at the same time
if i > 0 then delete (items i thru fileCount of theFiles)
-- Recurse through the folder's subfolders
set theSubfolders to every folder of theFolder
end tell
repeat with thisSubfolder in theSubfolders
deleteOldFiles(thisSubfolder, cutoffDate)
end repeat
end deleteOldFiles
I’m assuming that you only want to look in a certain folder, otherwise you would be deleting system files and others…
set theFolder to choose folder with prompt "Select search folder"
set numberOfDaysOld to 7 -- change to number of days back you want to go
tell application "Finder" to set theCount to count of items in theFolder
set currentDate to (current date) - days * numberOfDaysOld
display dialog currentDate as string
repeat until theCount <= 0
tell application "Finder" to set thisItem to item theCount of theFolder
set theInfo to info for thisItem
set isaFolder to folder of theInfo
if not isaFolder then
display dialog (name of theInfo)
set theDate to modification date of theInfo
if theDate < currentDate then
tell application "Finder" to move thisItem to the trash
end if
end if
set theCount to theCount - 1
end repeat
I think this will do what you are looking for
Good luck,
D
I’m using the script from ‘D’ (because the other one doesn’t work with me…), so now I use this:
set theFolder to choose folder with prompt “Select search folder”
set numberOfDaysOld to 7 – change to number of days back you want to go
tell application “Finder” to set theCount to count of items in theFolder
set currentDate to (current date) - days * numberOfDaysOld
display dialog currentDate as string
repeat until theCount <= 0
tell application “Finder” to set thisItem to item theCount of theFolder
set theInfo to info for thisItem
set isaFolder to folder of theInfo
if not isaFolder then
display dialog (name of theInfo)
set theDate to modification date of theInfo
if theDate < currentDate then
tell application "Finder" to move thisItem to the trash
end if
end if
set theCount to theCount - 1
end repeat
But I want some changes…
I want to define my folder, because it is always the same folder I want to ‘scan’ …
and I want that the script ‘scans’ also the subfolders in it …
and I want that this script is repeating (every 24 hours) …
Can mr ‘D’ change this for me?
Thanks !!!