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: Emmanuel LEVY <email@hidden>
- Date: Sat, 03 Nov 2012 01:37:57 +0100
You're mostly right.
The "paragraphs of" trick is silly, it was just intended to make it short, but you're right it's not a working script.
Now "why get a list of all the files", that's because we did not find another way to force the system to update the information. Our framework is a bit special, it's a xfs volume shared over nfs, and when you rename or move a file it's incredibly fast but also incredibly... subtle, in the sense that you have to make much effort to get the up to date information. For instance, "blah blah blah as alias" doesn't find the file in its new location.
Thanks much for taking the time to reply. I'm sure your input will help us somehow.
All the best,
Emmanuel
On Nov 3, 2012, at 12:11 AM, Ron Hunsinger wrote:
>
> 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
_______________________________________________
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