Delete all files except most recent X

Hello,

I’m trying to create an applescript that will delete all files after the most recent X files in a folder. The applescript is sent from Filemaker so the folder location and number of recent files to keep is set by the user in the Filemaker solution.

I’ve managed to get a list made with the files in the folder sorted so that 1 is the most recent. I cant figure out how to delete files x +1 thru the oldest file. Not sure if I need to loop through or if there is a nice concise way to just delete the files in one command?

Any help is appreciated

Code below:


set sourceFolder to "Macintosh HD:Users:Colin:Dropbox:CEC LD Templates:Group List Creator:00 Working Group Creator:Backup:"

set numFilesToKeep to 5


tell application "Finder"
	try
		set sortedList to sort (get files of folder sourceFolder) by modification date
		set sortedList to the reverse of sortedList
	end try
	set listCount to count sortedList
	--Now I need to delete items numFilesToKeep + 1 thru listCount?
	
end tell

Thank you

Hi there,

Does this do it?


set sourceFolder to "Macintosh HD:Users:Colin:Dropbox:CEC LD Templates:Group List Creator:00 Working Group Creator:Backup:" as text

set numFilesToKeep to 5

tell application "Finder"
	
	try
		set sortedList to sort (get files of folder sourceFolder) by modification date
		set sortedList to the reverse of sortedList
	end try
	
	set listCount to count sortedList
	
	if listCount > numFilesToKeep then
		delete items 6 thru listCount of sortedList
	end if
	
end tell

Alternate proposal.

set sourceFolder to "Macintosh HD:Users:Colin:Dropbox:CEC LD Templates:Group List Creator:00 Working Group Creator:Backup:" as text

set numFilesToKeep to 5

tell application "Finder"
	
	try
		set sortedList to sort (get files of folder sourceFolder) by modification date
		set sortedList to the reverse of sortedList
	end try
	
	set listCount to count sortedList
	set oneMore to numFilesToKeep + 1
	if listCount > numFilesToKeep then
		repeat (listCount - numFilesToKeep) times
			
			delete item oneMore of sortedList
			
		end repeat
	end if
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 26 novembre 2018 17:54:01

Thank you very much this solution worked exactly as expected. I had tried similar language but was using “to” instead of “thru”.

Hi there,

Glad to hear it worked.

Have a look at Yvan’s solution too because there’s a nice little bit that checks if there are files to delete.


set listCount to count sortedList
   set oneMore to numFilesToKeep + 1

   if listCount > numFilesToKeep then
-- delete the files
end if

You may want to include something that checks you’re deleting files from the correct folder too.

HTH

Hey Yvan, I get an error here. I guess that the script is trying to delete several times the item " oneMore of sortedList". After the first loop, this item doesn’t not exist anymore in the list “sortedList”. And this might trigger an error 1728: Finder got an error: Can’t get document file.

If I change it into:

set x to 0
		repeat (listCount - numFilesToKeep) times
			delete item (oneMore + x) of sortedList
			set x to (x + 1)
		end repeat

Then it should be fine.

Hi.

I think Yvan was confusing deleting the nth item in a list with deleting the nth item in a folder. If the nth item in a folder is deleted, the (n + 1)th item becomes the nth, so you can keep deleting the nth item the required number of times. In a list, the file represented by the nth item may be deleted, but the reference to it in the list remains.

A simpler repeat would be:

-- This only executes if listCount > numFilesToKeep.
repeat with i from (numFilesToKeep + 1) to listCount
	delete item i of sortedList
end repeat

Hi Nigel,

When deleting files is it also acceptable to use thru ?


if listCount > numFilesToKeep then
       delete items 6 thru listCount of sortedList
   end if

Hi TecNik.

Yes. That works fine. But it’s because the Finder’s delete command accepts AppleScript lists of things to delete (which isn’t mentioned in its dictionary) rather than being an AppleScript thing per se. I imagine that items 6 thru listCount of sortedList is executed by the core language and produces another list containing just the specified items, which is then passed to the Finder.

Hi Nigel,

Thanks for the explanation.
Maybe when I get a minute I’ll see if there’s a difference execution wise.

Thanks again for the help.

I thought I’d give this a try. My suggestion is mostly a fancied-up version of TecNik’s script.

set sourceFolder to "Macintosh HD:Users:Colin:Dropbox:CEC LD Templates:Group List Creator:00 Working Group Creator:Backup:"

set numFilesToKeep to 5

tell application "Finder"
	set sortedList to sort (files of folder sourceFolder) by modification date
end tell

set fileCount to (count sortedList)

if fileCount ≤ numFilesToKeep then
	display dialog "The source folder contains " & numFilesToKeep & " or fewer files." buttons {"OK"} cancel button 1
else
	set filesToDelete to items 1 thru (fileCount - numFilesToKeep) of sortedList
end if

tell application "Finder" to delete filesToDelete