Help with a script to keep 30 most recent files in a folder


I made a script that backups my work every day in a ZIP file to a file server in a specific folder.

But I would need to keep only the 30 most recent ZIP files in that folder.

Could someone help me in the construction of the “stage” of this script that will delete the oldest files to keep only the 30 most recent ones?

Thanks a lot in advance for the help…


Here is my first draft to do this… But it doen’t work…

set counterVariable to 0
set stopValue to 30
set stepValue to -1

set DossierSauvegarde to alias "Macintosh HD:Users:andrew:Documents:Scripts:"

tell application "Finder" to set ListeFichiers to sort (every item of DossierSauvegarde whose name extension is "scpt") by creation date -- most recent first

set startValue to count items in ListeFichiers

if startValue ≥ 30 then
	repeat with counterVariable from startValue to stopValue by stepValue
		set theFile to item -1 of ListeFichiers
		set theinfo to creation date of (info for theFile as alias) as text
		tell application "Finder"
			delete DossierSauvegarde & theFile
		end tell
	end repeat
end if