Change modified date of folder based on file's modified date

Hello,

I am looking for the best way to change the modified date of a folder to the newest modified of a file in that folder.

Example:

Master Directory Modified 5/18/2016 18:33
±->Folder_A Modified 5/18/2016 18:33
±–>Folder A.1 Modified 5/18/2016 18:33
File A.1.1 Modified 6/2/2013 01:00
File A.1.2 Modified 6/1/2013 01:23
±–>Folder A.2
File A.2.1 Modified 5/3/2015 05:33
±->Folder_B Modified 5/18/2016 18:33
±–>Folder B.1 Modified 5/18/2016 18:33
File B.1.1 Modified 12/31/2012 04:33

Both Folder_A and A.2 should have a Modified date of 5/3/2015 05:33 because that is the last modified file in that set of folders.

Both Folder_B and B.1 should have a Modified Date of 12/31/2012 04:33.

This is what I have, partly edited from a folder action.


on open
	tell application "Finder"
		set this_folder to (choose folder)
		set folder_list to items of folder this_folder
		repeat with new_file in folder_list
			try
				set latestFile to last item of (sort (get files of (this_folder)) by modification date) as alias
				set modification date of this_folder to latestFile's modification date
			end try
		end repeat
	end tell
end open

It works as a folder action, but it only changed the folder that I added a file to. It needs to run when doubt-clicked or when the folder is dropped on the app. Many sub folders too. Some of the information I see online is many years old and I’m not sure where to troubleshoot. I’m completely lost now. :frowning:

Hello again,

Is this not possible? Did I make a mistake posting here? I searched the site for a solution, but couldn’t find one. Is there a term for what I am trying to do that I might not be aware of, so I can continue searching elsewhere?

Thank you

Hi,

saved as an application this script works as applet and droplet and considers all subfolders


on run
	set baseFolder to choose folder
	processFolder(baseFolder)
end run

on open theItems
	repeat with anItem in theItems
		tell application "Finder" to set isFolder to class of anItem is folder
		if isFolder then processFolder(contents of anItem)
	end repeat
end open

on processFolder(aFolder)
	tell application "Finder"
		try
			set latestFile to last item of (sort (get files of aFolder) by modification date)
			set modification date of aFolder to latestFile's modification date
		end try
		set subfolders to folders of aFolder
		repeat with subfolder in subfolders
			my processFolder(contents of subfolder)
		end repeat
	end tell
end processFolder


Excellent! Thank you so much. I guess the processFolder and if isFolder is something I need to read up on. :stuck_out_tongue:

The script should find last modified item (that is, file or folder) of folders and subfolders in the entire contents of every processed folder inside the recursive process.


on run
	set baseFolder to choose folder
	processFolder(baseFolder)
end run

on open theItems
	repeat with anItem in theItems
		tell application "Finder" to set isFolder to class of anItem is folder
		if isFolder then processFolder(contents of anItem)
	end repeat
end open

on processFolder(aFolder)
	tell application "Finder"
		try
			set latestFinderItem to last item of (sort (get items of entire contents of aFolder) by modification date) -- EDITED
			set modification date of aFolder to latestFinderItem's modification date -- EDITED
		end try
		set subfolders to folders of aFolder
		repeat with subfolder in subfolders
			my processFolder(contents of subfolder)
		end repeat
	end tell
end processFolder