Results 1 to 4 of 4

Thread: Folder deletion problem

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Location
    England
    Posts
    17

    Folder deletion problem

    I'm trying to make a program which will delete a folder under the directory "C:\Documents and Settings\user_here\Application Data"

    I have tried "kill" and "rmdir" but I get error 75 msg" It wants me to kill each file under the dir before I can remove any folder.

    I'm not trying to kill the app. data folder just a few folders underneath it.

    If anyone has any ideas, reply.

    Thanks

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Location
    England
    Posts
    17

    Re: Folder deletion problem

    Nevermind found:

    VB Code:
    1. Private Sub Form_Load()
    2. 'Replace the 'C:\MyDir' below with the name of the directory you want to delete.
    3. x = DelTree("C:\DIR")
    4. Select Case x
    5. Case 0: MsgBox "Deleted"
    6. Case -1: MsgBox "Invalid Directory"
    7. Case Else: MsgBox "An Error was occured"
    8. End Select
    9. End Sub
    10.  
    11. Function DelTree(ByVal strDir As String) As Long
    12. Dim x As Long
    13. Dim intAttr As Integer
    14. Dim strAllDirs As String
    15. Dim strFile As String
    16. DelTree = -1
    17. On Error Resume Next
    18. strDir = Trim$(strDir)
    19. If Len(strDir) = 0 Then Exit Function
    20. If Right$(strDir, 1) = "\" Then strDir = Left$(strDir, Len(strDir) - 1)
    21. If InStr(strDir, "\") = 0 Then Exit Function
    22. intAttr = GetAttr(strDir)
    23. If (intAttr And vbDirectory) = 0 Then Exit Function
    24. strFile = Dir$(strDir & "\*.*", vbSystem Or vbDirectory Or vbHidden)
    25. Do While Len(strFile)
    26. If strFile <> "." And strFile <> ".." Then
    27.   intAttr = GetAttr(strDir & "\" & strFile)
    28.   If (intAttr And vbDirectory) Then
    29.    strAllDirs = strAllDirs & strFile & Chr$(0)
    30.   Else
    31.    If intAttr <> vbNormal Then
    32.     SetAttr strDir & "\" & strFile, vbNormal
    33.     If Err Then DelTree = Err: Exit Function
    34.    End If
    35.    Kill strDir & "\" & strFile
    36.    If Err Then DelTree = Err: Exit Function
    37.   End If
    38. End If
    39. strFile = Dir$
    40. Loop
    41. Do While Len(strAllDirs)
    42. x = InStr(strAllDirs, Chr$(0))
    43. strFile = Left$(strAllDirs, x - 1)
    44. strAllDirs = Mid$(strAllDirs, x + 1)
    45. x = DelTree(strDir & "\" & strFile)
    46. If x Then DelTree = x: Exit Function
    47. Loop
    48. RmDir strDir
    49. If Err Then
    50. DelTree = Err
    51. Else
    52. DelTree = 0
    53. End If
    54. End Function

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Folder deletion problem

    or you could just use FSO:
    VB Code:
    1. ' Set reference to Microsoft Scripting Runtime:
    2. Private Sub Command1_Click()
    3.     Dim fso As FileSystemObject
    4.     Set fso = New FileSystemObject
    5.     fso.DeleteFolder "C:\mytest", True
    6.     Set fso = Nothing
    7. End Sub

  4. #4
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Folder deletion problem

    Here is the simple code:
    VB Code:
    1. Public Sub RemoveDirectory(ByVal sDir As String)
    2.     Shell "Cmd.exe /C Rmdir /S /Q " & sDir
    3. End Sub
    4. 'Call this routine as:
    5. 'Call RemoveDirectory("C:\Temp\Test\")
    CS

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