Re: Checking on the existence of a file
Re: Checking on the existence of a file
- Subject: Re: Checking on the existence of a file
- From: Ron Hunsinger <email@hidden>
- Date: Fri, 02 Nov 2012 16:11:34 -0700
On Nov 1, 2012, at 5:40 PM, Emmanuel LEVY <email@hidden> wrote:
> FWIW, we do have to check existence of files several millions times a day, and sometimes in difficult conditions (for instance, we have to check from machine A the existence of a file which was just created on machine B by machine C).
>
> The most reliable way we know uses ls:
>
> theFileName is in (get paragraphs of (do shell script "ls " & quoted form of theFolderPath))
>
> returns true if the file exists, false if it does not while the folder exists, and it fails if the folder does not exist.
That's rather roundabout (read: expensive), and not entirely reliable.
As for reliability, remember that filenames are permitted to contain newline and return characters, so "get paragraphs of ..." may split the output in the wrong place.
But as for efficiency, why get a list of all the files in a directory when you're only interested in one? "do shell script ..." is going to invoke a copy of /bin/sh, which is then going to invoke a copy of /bin/ls, which is then going to get a list of all the filenames in the folder, then sort that list (because /bin/ls always sorts its output), then return the whole list to you as a single text object, and you still have to break it into paragraphs to get it back into a list again, and then compare your filename with each item of that list to see if you get a match.
If you want to get the shell to tell you if a file exists, why not just ask the shell that very specific question:
set theFilePath to theFolderPath & "/" & theFileName
do shell script "if [[ -e " & (quoted form of theFilePath) & " ]]; then echo YES; else echo NO; fi"
The copy of /bin/sh that you invoke can handle both [[ ... ]] and echo internally, without firing up any external commands (but don't use [ ... ] single brackets, which are just an alias for /bin/test); you get information about just the one file you're interested in, rather than the whole shebang; the only response you get back is the single word YES or NO, so no need to split into paragraphs and search a list.
-Ron Hunsinger
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden