Dir$() - Sharing Violation
Ok, this is wierd (or maybe I'm a dunce)
Objective: decide whether or not to delete a folder.
I test for files in a Folder, if (at least) one file exists, I don't delete the folder; whereas if there are no files I delete it.
I use the following:
VB Code:
Private Sub Command1_Click()
If Dir$("C:\Test\Bruce\*.*", vbNormal) = vbNullString Then
Debug.Print "Folder Empty"
Else
Debug.Print "File(s) Exist"
End If
End Sub
If there is a file, I get the Debug (which is expected). But, if I try to (manualy) delete the Folder, I get a 'Sharing Violation'! - Contradicory to my Objective, but I has me wondering whay I can't delete a Folder once I run this code.......
Here is the funny thing, If I rem out the else, It can delete the Folder???
VB Code:
Private Sub Command1_Click()
If Dir$("C:\Test\Bruce\*.*", vbNormal) = vbNullString Then
Debug.Print "Folder Empty"
' Else
' Debug.Print "File(s) Exist"
End If
End Sub
There must be an instance of the Folder open somehow post Dir$().
I noticed this has been asked before, but no answer has been given.
(Please note, I'm not after a workaround/alternate method, just wondering what VB is doing in this regared) :)
Cheers,
Re: Dir$() - Sharing Violation
I'm not sure how relevent this is to that procedure, but I find that if one of my command buttons / form loads / etc would delete files from a folder and then delete the folder, i get the same kind of error. I can remove a directory fine, but if it's in the same button as one that deletes files from that folder (IOW, if it's part of the same operation), it never works, even if I use a timer or the Sleep function.
Re: Dir$() - Sharing Violation
Yeah, it's a bit of a wierd one. But I think what's happening is if the Dir$(..) function returns something (ie it found a file) it actually places a lock on the folder. If you use Dir$() again to move to a different folder, everything's OK
VB Code:
Option Explicit
Private Sub Command1_Click()
If Dir$("C:\Test\Bruce\*.*", vbNormal) = vbNullString Then
Debug.Print "Folder Empty"
Else
Debug.Print "File(s) Exist"
Dir$ "c:\" ' This releases the lock on C:\Test\Bruce
End If
End Sub
That's my theory, and I'm sticking to it :bigyello:
Re: Dir$() - Sharing Violation
G'Day Pete :wave:,
Concur. (good theory :))
I had been able to release the folder too (by closing the App :0)).
I am curious tho as to what is going on behind the sceens. You would think that it would keep the lock in both cases if it was going to lock for at all.
Cheers,
Re: Dir$() - Sharing Violation
Quote:
Originally Posted by Bruce Fox
You would think that it would keep the lock in both cases if it was going to lock for at all.
Yes, you are right. You would think that, but this is one of those kind of quirky things about Dir$ that you need to aware lest you get bitten and not know why.
Re: Dir$() - Sharing Violation
Bruce Fox,
The reason that you cannot delete the folder is that once you do the Dir the folder is open and will not be closed until you move to another folder. You will find out that if you just dor a Dir of another folder you will close the current folder.
Re: Dir$() - Sharing Violation
G'Day randen,
Quote:
You will find out that if you just dor a Dir of another folder you will close the current folder.
Yep, thats what Pete alluded to aswell.
Hack, your right, it is a trap (I inadvertantly stumbled onto it).
I'l glad there has been some discussion on this as I didn't have too much luck when I found similar posts.