Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot delete folder that has a sharing violation 3

Status
Not open for further replies.

yeungsprite

Programmer
Nov 28, 2002
25
US
Hi,

I am having a problem deleting a folder that is created within VB. When I try and delete the folder, I get a message: "Run-time error '75': Path/File access error". The folder cannot be deleted manually either, and displays the message "There has been a sharing violation. The source or destination file may be in use". The issue may be able to be resolved by using the ChDir command, but I am unsure of how to accomplish this. I have included sample code at the bottom.

Regards,
Andrew

-----------------------------------
Dim Reply As Integer
Dim tmpfolder As String
tmpfolder = "C:\tempfolder"

' check if directory exists
If Len(Dir(tmpfolder, vbDirectory)) > 0 Then
Reply = MsgBox("Directory exists, Delete?", vbYesNo, "Delete Folder & Contents and create New Folder?")

If Reply = 6 Then ' Yes delete folder

If Len(Dir(tmpfolder & "\", vbNormal)) > 0 Then 'not an empty directory
'delete files
Kill (tmpfolder & "\*.*")
End If
'delete folder
RmDir (tmpfolder) <---break point
Exit Sub
Else
If Reply = 7 Then ' No, don't delete folder
MsgBox (&quot;No Directory created&quot;)
Exit Sub
End If
End If
End If

'Create new directory
MkDir tmpfolder
 
The problem is that the Dir(tmpfolder & &quot;\&quot;, vbNormal) is placing the system in the folder (for recursive calls to Dir with no parameters). All you need to do is call Dir again with a parameter pointing to a different location. I put one into your sample which goes back to the folder search before calling the rmdir.

Dim Reply As Integer
Dim tmpfolder As String
tmpfolder = &quot;C:\tempfolder&quot;

' check if directory exists
If Len(Dir(tmpfolder, vbDirectory)) > 0 Then
Reply = MsgBox(&quot;Directory exists, Delete?&quot;, vbYesNo, &quot;Delete Folder & Contents and create New Folder?&quot;)

If Reply = 6 Then ' Yes delete folder

If Len(Dir(tmpfolder & &quot;\&quot;, vbNormal)) > 0 Then 'not an empty directory
'delete files
Kill (tmpfolder & &quot;\*.*&quot;)
Dir tmpfolder, vbDirectory
End If
'delete folder
RmDir (tmpfolder) '<---break point
Exit Sub
Else
If Reply = 7 Then ' No, don't delete folder
MsgBox (&quot;No Directory created&quot;)
Exit Sub
End If
End If
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top