Re: Get every file of the entire contents of a folder. Quickly.
Re: Get every file of the entire contents of a folder. Quickly.
- Subject: Re: Get every file of the entire contents of a folder. Quickly.
- From: kai <email@hidden>
- Date: Tue, 14 Oct 2003 06:57:42 +0100
on Mon, 13 Oct 2003 16:57:03 -0400, Paul Skinner wrote:
>
In trying 'entire contents of' and Satimage's 'list files' I am left
>
unsatisfied. I won't even try recursing the directories in AppleScript.
>
>
Satimage OSAX 2.5.5 won't go recursive OMM even though that is the
>
default, and 'entire contents of' is SO SLOW! So here's my current
>
solution. I think it runs at 'Ludicrous speed'. Returning 4960
>
unfiltered files in less than four seconds on my 667 PB G4, although I
>
can't compare it to Satimage due to the later's refusal to perform.
>
>
Can you break it? Offer improvements?
>
>
EveryFileOfFolder(choose folder)
>
EveryFileOfFolder({choose folder, "jpg"})
>
on EveryFileOfFolder(parameters)
>
set prevDelim to AppleScript's text item delimiters
>
set searchterm to ""
>
repeat with thisItem in (parameters as list)
>
try
>
set folderPath to ((thisItem as text) as alias)
>
on error
>
set searchterm to (thisItem as text)
>
end try
>
end repeat
>
>
set AppleScript's text item delimiters to ":"
>
set DiskName to return & text item 1 of (folderPath as text)
>
set AppleScript's text item delimiters to ""
>
set folderPath to quoted form of (POSIX path of folderPath)
>
try
>
set filelist to {}
>
if searchterm is not "" then
>
set filelist to (paragraphs of (do shell script "find " &
folderPath
>
& " ! -name '\\.*' -type f | tr -s '//' | tr '/' ':' | grep " &
>
searchterm))
>
else
>
set filelist to (paragraphs of (do shell script "find " &
folderPath
>
& " ! -name '\\.*' -type f | tr -s '//' | tr '/' ':'"))
>
end if
>
end try
>
>
if filelist is {} then
>
set AppleScript's text item delimiters to prevDelim
>
return filelist
>
end if
>
set AppleScript's text item delimiters to DiskName
>
set filelist to (DiskName & (filelist as text) as Unicode text)
>
set AppleScript's text item delimiters to return
>
set filelist to paragraphs 2 thru -2 of filelist
>
set AppleScript's text item delimiters to prevDelim
>
return filelist
>
end EveryFileOfFolder
I'd guess that most of the script's time is spent finding files, so any
speed enhancements from AS fine-tuning would probably be minor. However, a
few thoughts:
-------------------------------------------------------
(Any wrapped lines abutting the left edge of the window
should be reconnected to the end of the previous line)
-------------------------------------------------------
--=======================
on EveryFileOfFolder(p)
set termExists to p's class is list
if termExists then
set folderAlias to p's item 1
set searchterm to p's item 2
else
set folderAlias to p
end if
set folderPath to quoted form of (POSIX path of folderAlias)
try
if termExists then
set filelist to paragraphs of (do shell script "find " & folderPath & "
! -name '\\.*' -type f | tr -s '//' | tr '/' ':' | grep " & searchterm)
else
set filelist to paragraphs of (do shell script "find " & folderPath & "
! -name '\\.*' -type f | tr -s '//' | tr '/' ':'")
end if
on error
return {}
end try
set prevDelim to text item delimiters
set text item delimiters to ":"
set DiskName to return & text item 1 of (folderAlias as Unicode text)
set text item delimiters to DiskName
set filelist to rest of (DiskName & (filelist as Unicode text))'s
paragraphs
set text item delimiters to prevDelim
end EveryFileOfFolder
--=======================
If you wanted flexibility about the order in which parameters appear in the
calling statement, then you could change lines 4-5 from:
-----------------------------
set folderAlias to p's item 1
set searchterm to p's item 2
-----------------------------
to:
-----------------------------
set folderAlias to item 1 of p's aliases
set searchterm to item 1 of p's strings
-----------------------------
...which would obviously be a whisker slower - though still faster than a
repeat loop/try block, I suspect.
Rather disgracefully, I haven't yet had a chance to test/compare either
version - so I hope I've not introduced any typos/errors. (It looks to me as
if you might, by using 'paragraphs 2 thru -2 of filelist', have been losing
the last file found by the shell script - but I might be wrong about that.)
---
kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.