Re: Efficiently searching for file name
Re: Efficiently searching for file name
- Subject: Re: Efficiently searching for file name
- From: bill fancher <email@hidden>
- Date: Mon, 7 Oct 2002 07:19:38 -0700
On Sunday, October 6, 2002, at 08:36 PM, Brian Redman wrote:
If it makes the Unix find(1) command any more attractive you can
redirect the errors
so they aren't displayed. If you run it with /bin/sh the syntax is:
find / -x -name whatever 2>/dev/null
The "2>/dev/null" redirects standard error (file descriptor 2) output
to the bit bucket. The other
shells surely have the same functionality but I don't their syntax.
There's one hitch in AppleScript: find returns non-zero when it gets
errors along the way. The do shell script command looks at the return
value of the expression, and based on that, decides whether to display
an error. If the return value is 0, there's no error and it just copies
standard out to the result. If the return value is non-zero, it
displays an error with standard output, or, if that's empty, standard
error as the error message (or maybe it tries standard error first, I
didn't check). The upshot is that if you try to search where you're not
allowed to, your valid search results are displayed as an error message.
One fix is piping the output from find through cat (sounds painful for
cat):
do shell script "find /System -name 'Burgundy.*|cat'
cat just copies standard input to standard output here, so it always
succeeds and hence returns 0. Since it's last in the pipe, that's the
value that goes back to do shell script.
Faster still is "locate", which uses an index of pathnames. Drawbacks
are:
1) you've got to explicitly build the index before the first use by
manually running /usr/libexec/locate.updatedb. (I think that's still
true...) Once it's built, it will be updated by the weekly housekeeping
script.
2) by default the index is only updated once a week (though you can
update as often as you like by running /usr/libexec/locate.updatedb).
Example: find all files whose names end with ".txt" (that were on the
machine the last time the locate index was built):
do shell script "locate '*/*.txt'"
--
bill
_______________________________________________
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.