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: Paul Skinner <email@hidden>
- Date: Tue, 14 Oct 2003 10:36:14 -0400
On Tuesday, October 14, 2003, at 02:44 AM, kai wrote:
on 14/10/03 6:57 am, I wrote:
on EveryFileOfFolder(p)...
Come to think of it - I could have cleaned that up a bit better:
snip compressed, revised, script.
---
kai
kai, you're like an AppleScript coffee press.
I bet your parent's named you Kaisarandamajewaski and you immediately
shortened it to kai. : )
Thanks for your input!
On Tuesday, October 14, 2003, at 01:57 AM, kai wrote:
Id guess that most of the scripts time is spent finding files, so
any speed enhancements from AS fine-tuning would probably be minor.
Remarkably, the shell is so fast (unless you traverse directories that
you don't have permissions for) that earlier versions spent more time
in the TIDs conversions!
If you wanted flexibility about the order in which parameters appear
in the calling statement, then you could
snip
set folderAlias to item 1 of p's aliases
set searchterm to item 1 of p's strings
COOL! Why haven't I thought to try that before? I don't care to add
positional flexibility, but what a great method.
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.)
I WAS throwing away the last found file. Occasionally the last item is
just the disk name, I haven't fully groked this problem yet.
I prefer your method for assigning the variable parameters, although I
did add back the 'as text) as alias' coercion. this lets the user pass
a string, alias, or folder spec.
I had a bit of trouble with the 'rest of' statement and stack
overflows, so I reverted to the '2 thru -1' construction.
Unfortunately, there was a major bug in the code I wrote. Grep was not
the way to go for the containing search. I was getting hits where the
file path contained the search term. The 'find' shell command has it's
own switch for this. And the results are not only more accurate, but
they come back quicker!
EveryFileOfFolder({choose folder, ".jpg"})
on EveryFileOfFolder(parameters)
set termExists to parameters's class is list
if termExists then
set folderReference to (parameters's item 1 as text) as alias
set searchterm to parameters's item 2
else
set folderReference to (parameters as text) as alias
end if
set folderPath to quoted form of (POSIX path of folderReference)
set shellFind to "find " & folderPath & " ! -name '\\.*' -type f | tr
-s '//' | tr '/' ':'"
if termExists then set shellFind to "find " & folderPath & " ! -name
'\\.*' -name '*" & searchterm & "*' -type f | tr -s '//' | tr '/' ':'"
try
set filelist to paragraphs of (do shell script shellFind)
on error
return {}
end try
set prevDelim to text item delimiters
set text item delimiters to ":"
set DiskName to return & text item 1 of (folderReference as Unicode
text)
set 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 -1 of filelist
set text item delimiters to prevDelim
return filelist
end EveryFileOfFolder
PS
What a strange machine man is. You fill him with bread, wine, fish, and
radishes, and out come sighs, laughter and dreams.
-Esther Dyson
_______________________________________________
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.