Feedback welcomed...

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

Well, I made comments inline with the code based on what I could tell was going on. If you explain again what you’re trying to do, I might write an example of how I would do it for comparison.

– your ‘buttons’ and ‘default button’ parameters are the defaults anyway, so I might’ve left them off -mwt
set question to display dialog ¬
“Are you sure you want to delete these old back up files permanently?” buttons {“Cancel”, “Yes”} ¬
default button “Yes”

– the script will throw an error and end if the user chooses ‘Cancel’ anyway, so no need for these tests -mwt
set button_name to button returned of question
if button_name is “Yes” then
end if

–run main script
tell application “Finder”
activate

-- since you use the path to the folder more than once, and it's a contstant, you might put it
-- at in a global or property at the top of the script. this also makes it easier to adapt the
-- script later if you want to duplicate for other folders or something
-- 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

-- this loop isn't doing anything. you're not saving the dates anywhere, except for the last one.
-- every time you get a date you overwrite the one before it. if you want to keep these dates
-- you need to svae them in a list. also, you don't need to the 'info for' command. the finder already
-- knows the file's creation date, so just ask for it directly of the target file -mwt
-- 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

-- unfortunately, this won't work the way you're hoping, but it's more than I've got time for to explain simply
-- why not or what to do about it -mwt
-- 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
	-- might reword this dialog, something like: count_deleted_files & " files deleted."
	display dialog "You have chosen to delete " & count_deleted_files & " files."
else if button_name is "Cancel" then
	-- I don't understand why you would need to move any files. they're already in the folder; you don't 
	-- need to move them to where they already are! -mwt
	move files_to_delete to "[path to folder]:"
end if

end tell
– it’s too late! you already deleted the files -mwt
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

– I suppose you posted two different versions of your script accidentally?
–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