As a complete newcomer to scripting I wanted to try my hand at developing a script which would go to a particular folder and delete those files over a week old, but always ensuring that there were at least 5 files left behind.
Over the weekend I managed to get it working, and I’d really appreciate any thoughts from more experienced users about how it might be tightened up. Particularly I’m concerned that I may have inadvertently put in a line which doesn’t really work the way I anticipate (though I have tried it through many times)
A few bits were borrowed from postings on this site and adapted, for which many thanks.
Clive
set question to display dialog ¬
“Are you sure you want to delete these old back up files permanently?” buttons {“Cancel”, “Yes”} ¬
default button “Yes”
set button_name to button returned of question
if button_name is “Yes” then
end if
–run main script
tell application “Finder”
activate
– count items in folder
set variable_1 to count folder [path to folder]
set source_Folder to "[path to folder]:"
tell application "Finder"
set file_list to every file of folder source_Folder
end tell
-- now go through each of the files
repeat with aFile in file_list
set file_info to info for (aFile as alias)
set file_creation_date to creation date of file_info
---etc.
end repeat
-- sort what to keep, keeping all newer than 7 days old, and leaving 5 files remaining
if variable_1 is greater than 5 and file_creation_date is greater than ((current date) - 7 * days) then
set files_to_delete to every file of folder "[path to folder]:" whose creation date is less than ((current date) - 7 * days)
end if
--confirm deletion and delete, or return files to original folder
set question1 to display dialog ¬
"Are you absolutely sure you want to delete these files?" with icon 2 buttons {"No", "Yes"} ¬
default button "Yes"
set button_name to button returned of question1
set count_deleted_files to count files_to_delete
if button_name is "Yes" then
delete files_to_delete
display dialog "You have chosen to delete " & count_deleted_files & " files."
else if button_name is "Cancel" then
move files_to_delete to "[path to folder]:"
end if
end tellset question to display dialog ¬
“Are you sure you want to delete these old back up files permanently?” buttons {“Cancel”, “Yes”} ¬
default button “Yes”
set button_name to button returned of question
if button_name is “Yes” then
end if
–run main script
tell application “Finder”
activate
– count items in folder
set variable_1 to count folder [path to folder]
set source_Folder to "[path to folder]:"
tell application "Finder"
set file_list to every file of folder source_Folder
end tell
-- now go through each of the files
repeat with aFile in file_list
set file_info to info for (aFile as alias)
set file_creation_date to creation date of file_info
---etc.
end repeat
-- sort wheat from chaff based on 7 days old, and leaving 5 files remaining
if variable_1 is greater than 5 and file_creation_date is greater than ((current date) - 7 * days) then
set files_to_delete to every file of folder "[path to folder]:" whose creation date is less than ((current date) - 7 * days)
end if
--confirm deletion and delete, or return files to original folder
set question1 to display dialog ¬
"Are you absolutely sure you want to delete these files?" with icon 2 buttons {"No", "Yes"} ¬
default button "Yes"
set button_name to button returned of question1
set count_deleted_files to count files_to_delete
if button_name is "Yes" then
delete files_to_delete
display dialog "You have chosen to delete " & count_deleted_files & " files."
else if button_name is "Cancel" then
move files_to_delete to "[path to folder]:"
end if
end tell