CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Why can't i use RmDir to delete a folder?

    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
    Best Regards,

    Kevin Shen

  2. #2
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: Why can't i use RmDir to delete a folder?

    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


  3. #3
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Re: Why can't i use RmDir to delete a folder?

    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
    Best Regards,

    Kevin Shen

  4. #4
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: Why can't i use RmDir to delete a folder?

    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.



  5. #5
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: Why can't i use RmDir to delete a folder?

    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

    Good Luck,
    -Cool Bizs

  6. #6
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Re: Why can't i use RmDir to delete a folder?

    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
    Best Regards,

    Kevin Shen

  7. #7
    Join Date
    Mar 2001
    Location
    Australia
    Posts
    146

    Re: Why can't i use RmDir to delete a folder?

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured