|
-
Dec 11th, 2005, 05:21 PM
#1
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,
Last edited by Bruce Fox; Dec 11th, 2005 at 05:26 PM.
-
Dec 11th, 2005, 06:29 PM
#2
Addicted Member
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.
-
Dec 12th, 2005, 02:21 AM
#3
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
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Dec 12th, 2005, 05:05 AM
#4
Re: Dir$() - Sharing Violation
G'Day Pete ,
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,
-
Dec 12th, 2005, 07:50 AM
#5
Re: Dir$() - Sharing Violation
 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.
-
Dec 12th, 2005, 12:55 PM
#6
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.
-
Dec 12th, 2005, 03:44 PM
#7
Re: Dir$() - Sharing Violation
G'Day randen,
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|