Count Files By Year

I’ve been meaning to share forward for a while.

This script inventories a collection of folders. For each folder it counts the number of files with a particular creation year. I use this to see where my old files are. It can be easily modified to check modification date.


-- Change the base year as needed

set base_year to 1999
set current_year to year of (current date)
set folder_list to (choose folder with prompt "Pick the folders containing the files to count:" with multiple selections allowed)
set log_file to (path to desktop as string) & "File Count by year and folder"
set output_file_id to open for access file log_file with write permission
write (base_year as string) to output_file_id starting at eof
close access output_file_id
repeat with base_folder in folder_list
	tell application "Finder" to set file_count_by_year to {(name of base_folder as string)}
	repeat with this_year from base_year to current_year
		set start_date to date ("1/1/" & this_year)
		set end_date to date ("1/1/" & (this_year + 1))
		with timeout of (60 * minutes) seconds
			tell application "Finder" to set end of file_count_by_year to count of (every file of base_folder whose (creation date ≥ start_date) and (creation date < end_date))
		end timeout
	end repeat
	set output_file_id to open for access file log_file with write permission
	set text item delimiters to "|"
	write return & (file_count_by_year as string) to output_file_id starting at eof
	set text item delimiters to ""
	close access output_file_id
end repeat
say "Finished"
return