Home All Groups Group Topic Archive Search About

Testing for "Access Denied" without trying to change the file

Author
11 Mar 2006 9:34 AM
Giles
When a file is being used (e.g. by another program) it is sometimes not
possible to overwrite it, delete it etc. Is there a gentle way to test if it
is "Access Denied" without actually trying to overwrite (it and trapping the
error)? Because that seems rather like testing if something is glass by
hitting it with a hammer.
Thanks

Author
11 Mar 2006 9:58 AM
J French
On Sat, 11 Mar 2006 09:34:02 -0000, "Giles" <giles@nospam.com> wrote:

>When a file is being used (e.g. by another program) it is sometimes not
>possible to overwrite it, delete it etc. Is there a gentle way to test if it
>is "Access Denied" without actually trying to overwrite (it and trapping the
>error)? Because that seems rather like testing if something is glass by
>hitting it with a hammer.
>Thanks

Generally the trick is to open it for binary access, lock read write
- if that fails then something else has grabbed it

Use On Error Resume Next
Author
11 Mar 2006 10:48 AM
Giles
Show quote Hide quote
>"J French"  wrote
> On Sat, 11 Mar 2006 09:34:02 -0000, "Giles" wrote:
>
>>When a file is being used (e.g. by another program) it is sometimes not
>>possible to overwrite it, delete it etc. Is there a gentle way to test if
>>it
>>is "Access Denied" without actually trying to overwrite (it and trapping
>>the
>>error)? Because that seems rather like testing if something is glass by
>>hitting it with a hammer.
>>Thanks
>
> Generally the trick is to open it for binary access, lock read write
> - if that fails then something else has grabbed it
>
> Use On Error Resume Next

Sorry, don't quite get it.
Open FPathName For Binary As 1
setattr FPathName,vbreadonly
This generates an error because you have just opened it, not because it was
in use already. And being in use doesn't prevent opening it for binary.
Think I have misunderstood what you mean by lock read write?
Author
11 Mar 2006 12:54 PM
Larry Serflaten
"Giles" <giles@nospam.com> wrote

> > Generally the trick is to open it for binary access, lock read write
> > - if that fails then something else has grabbed it
>
> Sorry, don't quite get it.
> Open FPathName For Binary As 1
> setattr FPathName,vbreadonly
> This generates an error because you have just opened it, not because it was
> in use already. And being in use doesn't prevent opening it for binary.
> Think I have misunderstood what you mean by lock read write?

Open PathName For Binary Lock Read Write As #FileNumber

Check out Open (and FreeFile example) in VB Help...

LFS
Author
11 Mar 2006 2:36 PM
Giles
Thanks both very much for your help.
Author
11 Mar 2006 5:54 PM
Bob O`Bob
Giles wrote:
> When a file is being used (e.g. by another program) it is sometimes not
> possible to overwrite it, delete it etc. Is there a gentle way to test if it
> is "Access Denied" without actually trying to overwrite (it and trapping the
> error)? Because that seems rather like testing if something is glass by
> hitting it with a hammer.
> Thanks
>
>

I see you've already got probably-workable suggestions.

Personally, I like  " Name f As f " with error trapping.

I'm not certain it would suit all such uses, but it's been
helpful to me in the past.



    Bob
--