Newest file using Spotlight

How to get modification time of newest file in x folder hierarchy without stepping thru every folder, maybe Spotlight can do this?

This folder has lots of files and folders so Spotlight might be best and fast.

If Spotlight cant do this then what else could be used?

This most likely can be done simpler or better,
But here is a modified old script I had.

Change the Path ( et thePrintpath to (path to desktop folder from user domain) & “testf:” as string)
editI have also set it to look for last file even if a folder is the last modified item



set c to "Folder"
set counter to 0

my lastFile_(c, counter)
on lastFile_(c, counter)
	set counter to counter + 1
	set thePrintpath to (path to desktop folder from user domain) & "testf:" as string
	set thePrintpath to POSIX path of thePrintpath
	set LastFile to paragraphs of (do shell script "cd " & thePrintpath & " ; ls -t1| perl -pe 'exit if $. > " & counter & "'")
	set LastFile to item counter of LastFile
	
	set acolonpath to POSIX file (thePrintpath & LastFile) as alias
	
	set c to (kind of (info for acolonpath))
	
	if c is "Folder" then
		my lastFile_(c, counter)
	else
		return (acolonpath)
	end if
end lastFile_

Thanks for this code.

My folder path has spaces, and it fails. It is in hard drive named “Macintosh HD” and last folder in path has also space.

This should take care of it. :wink:

set c to "Folder"
set counter to 0

my lastFile_(c, counter)
on lastFile_(c, counter)
	set counter to counter + 1
	set thetarget to (path to desktop folder from user domain) & "testf:untitled folder:" as string
	set thetarget to POSIX path of thetarget
	(* use sed to escape spaces in thetarget folder *)
	set thetargetSEDpath to (do shell script "echo " & quoted form of thetarget & "|sed 's/\\ /\\\\ /g'")
	set LastFile to paragraphs of (do shell script "cd " & thetargetSEDpath & " ; ls -t1| perl -pe 'exit if $. > " & counter & "'")
	set LastFile to item counter of LastFile
	
	set acolonpath to POSIX file (thetarget & LastFile) as alias
	
	set c to (kind of (info for acolonpath))
	
	if c is "Folder" then
		my lastFile_(c, counter)
	else
		return (acolonpath)
	end if
end lastFile_