Click to See Complete Forum and Search --> : Why can't i use RmDir to delete a folder?


kevin shen
March 29th, 2001, 08:24 PM
I've used kill to delete all the files in the folder, then use RmDir to delete the folder. But always show error "File/Path Access Error". Why?

Thank you!

Best Regards,

Kevin Shen

GungaDin
March 29th, 2001, 09:36 PM
I don't think that Kill will kill hidden files so it is possible that you have hidden files in the directory that aren't being removed. The following code will unhide all hidden files, so you should then be able to delete them. (Be a bit careful though. Hidden files are generally hidden for a reason :))



Dim theDir as string
Dim thePath as string

thePath = "C:\Temp\"
theDir = Dir(thePath & "*.*", vbHidden)

Do While theDir <> ""
SetAttr thePath & theDir, vbNormal
theDir = Dir
Loop

Kill thePath & "*.*"

RmDir thePath




It is also possible that you may have other folders in this folder that could be causing problems.

Hope this helps,

Nathan Liebke

kevin shen
March 29th, 2001, 09:49 PM
I don't think there's hidden files in the folder. Do you think there's other reason can cause the error? Thank you!

Best Regards,

Kevin Shen

GungaDin
March 29th, 2001, 10:43 PM
Are you 100% sure that there are no hidden files or hidden folders?

Is this a folder on your PC or on a network drive? If it is on a network drive it is possible that you may not have permissions to delete the folder.

If you are still having problems, send through the code and I'll have a look.

coolbiz
March 30th, 2001, 10:49 AM
Not sure how you code it but if you follow the below sequence it should also give you the same error:

1. Set current directory to the target directory
2. Delete all the files in the current directory
3. Delete current directory

This will fail since your process is in that directory. Between 2 and 3, insert: Set current directory up 1 level.

Hope this helps,
-Cool Bizs

kevin shen
March 30th, 2001, 12:28 PM
My code is below:

If MsgBoxYesNo("Are you sure the Test Record (" & FrmDataFolder.TreeViewFolderList.SelectedItem.Key & ") will be deleted?", "Data Record") then
Dim sDir as string
sDir = Dir(stFolderClickPath & "\*.*")
If len(sDir) then
Kill stFolderClickPath & "\*.*"
End If
RmDir stFolderClickPath
FrmDataFolder.TreeViewFolderList.Nodes.Remove (FrmDataFolder.TreeViewFolderList.SelectedItem.Key)
FrmDataFolder.TreeViewFolderList.Nodes.Item("RootDataFolder").Selected = true
else
GoTo Cleanup
End If

Cleanup:







stFolderClickPath is the folder path, the other is for treeview control. I did not delete treeview code for maybe this will cause error?

Thank you!

Best Regards,

Kevin Shen

GungaDin
March 31st, 2001, 11:49 PM
Did the other piece of advice about making sure that you aren't actually in the directory you are trying to delete help?

If that didn't fix it, I would check to ensure that you don't have any sub-folders in the folder in question as Kill obviously won't kill sub-folders.

The Microsoft Scripting Runtime (scrrun.dll) has a DeleteFolder function that will kill all sub-folders and files (even hidden files). The code is:



Dim fso as new FileSystemObject

fso.DeleteFolder stFolderClickPath, true






This will still fail however if you are in the folder being deleted but it is more reliable in killing folders as you can skip the step of actually deleting all files.

You have to be careful though as it is fairly brutal in its removal of a folder.

Hope this helps,

Nathan Liebke