Getting list of recent files in folders and subfolders

Hi

I am trying to get a list of all files (visible ones only) that are less than x days old on a volume/in a folder.

I have this:

tell application "Finder"
	set SearchFolder to "MacBook Pro:Users:macbookwork:Documents:ScanSnap:"
	set FilesToCopy1 to (every file of entire contents of folder SearchFolder whose modification date > ((current date) - 14 * days))
end tell

It works, so far as it goes, but is slow.

I have been playing around with others’ scripting here: http://macscripter.net/viewtopic.php?id=29183

But cannot seem to get it to work. The script at the above referred to post seems to return filepaths with an extra backslash just before the filename in each case. It does it very quickly though:)

Can anyone help?

Hello.

Try playing with the script in post #1 in This thread

Thanks

I have this so far:


set FilestoCopy to {}
set theFolder to "MacBook Pro:Users:macbookwork:documents:"
set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
	try
		if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
	end try
end repeat

Pretty quick. At least much quicker than using Finder or System Events…

Need to add the copy routine but that will be tomorrow.

I am signing off for now:)

you can search, filter and copy in one line


set documentsFolder to POSIX path of (path to documents folder)
set destinationFolder to POSIX path of (choose folder)
do shell script "/usr/bin/find " & quoted form of documentsFolder & " -type f ! -name '.*' -mtime -14d -print -exec /bin/cp {} " & quoted form of destinationFolder & " \\;"

-mtime -14d finds all files with modification date within the last 14 days
-print -exec passes the file paths to the following command (cp)

Thanks, Stefan

Extra helpful

Hi there

I have a further query. I have decided to make aliases to the files in question, rather than copy them.

I have this script, which works as far as it goes:

set FilestoCopy to {}
set theFolder to "MacBook Pro:Users:macbookwork:documents:"
set AliasFolder to "MacBook Pro:Users:macbookwork:documents:aliases:"
set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
	try
		if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
	end try
end repeat
tell application "Finder"
	repeat with eachfile in FilestoCopy
		make alias to eachfile at AliasFolder
	end repeat
end tell

It creates aliases as expected. But if I run it a second time, it creates alias2, and if I run it again, it creates alias 3 etc etc

How do I test to see if the alias already exists before executing the make alias command?

I have tried various syntactic ways, but none work.

Cheers

Maybe:

set FilestoCopy to {}

set theFolder to path to documents folder
set AliasFolder to theFolder & "aliases:" as string


set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
	try
		if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
	end try
end repeat
tell application "Finder"
	repeat with eachfile in FilestoCopy
		set theFile to name of (eachfile as alias)
		log theFile
		if not (exists (AliasFolder & theFile)) then
			make alias to eachfile at AliasFolder
		end if
	end repeat
end tell

Fantastic. Thanks, Mark

For my info, can I ask, what is the function/purpose/role of “log theFile”?

Cheers

Hi Mark

Just noticed that your script does not work for Pages documents. I guess this is because they are, in effect, packages.

Any way around this?

Mark

Sorry to revert. But I am having difficulties.

I have pointed your script to different folders:

set FilestoCopy to {}

set theFolder to ((path to home folder as text) & "Downloads:Testfolder" as alias)
set AliasFolder to (path to desktop folder as text) & "Aliases"


set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")
repeat with oneFile in theFiles
	try
		if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
	end try
end repeat
tell application "Finder"
	repeat with eachfile in FilestoCopy
		set theFile to name of (eachfile as alias)
		log theFile
		if not (exists (AliasFolder & theFile)) then
			make alias to eachfile at AliasFolder
		end if
	end repeat
end tell

And now it fails to spot any duplicate aliases:( It recreates fresh aliases every time I run the script.

Any ideas why this might be so?

the line

if not (exists (AliasFolder & theFile)) then

will always evaluate true because the colon separator is missing at the end of this line


set AliasFolder to (path to desktop folder as text) & "Aliases:"

Consider that in terms of AppleScript the class of a Finder alias is alias file

Thanks, Stefan

Works fine now.

Don’t suppose you have a suggestion to work around packages - the script will systematically duplicate an alias for Page documents (and Numbers documents - and I presume all other types of document that are in fact packages).

Many thanks

You could filter package items with the -path switch of the find command

I don’t think that is the answer. I want to create aliases for the Pages documents (so need to be included in find command), but I only want to create alias once.

The problem seems to be with the

.

When I look at the values of theFile, nothing untoward looks different between a PDF and a Pages document (for example). Yet the existence text does not “see” the Pages document. Whereas it “sees” the PDF.

actually the line checks for the existence of a string, not for a Finder item, so try

if not (exists item (AliasFolder & theFile))

Just tried it. Same result. Multiple copies of Pages/Numbers aliases:(

Tried using

if not (exists file (AliasFolder & theFile)) then

Also, same result:(

Weird

As a hack, I could have a repeat loop at the end of the script delete every item in AliasFolder whose name contains alias - the first alias created has exactly the same name as the original file - subsequent aliases are names “…alias”, “… alias2” etc etc. So this would “work”, albeit in a rather messy fashion.

I will do this as last resort, if mystery cannot be solved.

and here is my “hack”:

set aliasList to every item in folder AliasFolder
	repeat with eachfile in aliasList
		if name of (eachfile as alias) contains "alias" then
			do shell script ("rm " & POSIX path of eachfile as alias)
		end if
	end repeat

Except that the shell script returns an error - file not found. I am trying to delete the offending aliases WITHOUT going to the trash: ie a permanent delete.

What do I have wrong in the above?

Thanks for your help

OK. I have it, complete with hack to avoid duplication of alias files:

set FilestoCopy to {}
set theFolder to ((path to home folder as text) & "Downloads:Applications" as alias)
set AliasFolder to (path to desktop folder as text) & "Aliases:"

set theFiles to paragraphs of (do shell script "find " & quoted form of POSIX path of theFolder & " -type f ! -name '.*'")

repeat with oneFile in theFiles
	try
		if modification date of (info for (oneFile as POSIX file)) > ((current date) - 14 * days) then set end of FilestoCopy to oneFile as POSIX file
	end try
end repeat

tell application "System Events"
	
	repeat with eachfile in FilestoCopy
		set theFile to name of (eachfile as alias)
		log theFile
		if not (exists item (AliasFolder & theFile)) then
			tell application "Finder" to make alias to eachfile at AliasFolder
		end if
	end repeat
	
	set aliasList to every item in folder AliasFolder
	
	repeat with eachfile in aliasList
		if name of (eachfile as alias) contains "alias" then
			my deleteFile(eachfile as alias)
		end if
	end repeat
	
end tell

on deleteFile(filename)
	do shell script "rm -Rf " & quoted form of POSIX path of filename
end deleteFile




H,
I am a bit confused. Are you talking about foo.pages and foo.numbers

If so the original script I wrote works with them ?