CodeItBetter Programming Another VB Programming Blog

How to delete the given directory and all the files it contains.

Posted on July 11, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'File/Folder Handling - How to delete the given directory and all the files it contains.
Public Function fnDeleteDirectory(ByVal Dir_Name As String)
    On Error Resume Next
    Dim file_name As String
    Dim files As Collection
    Dim i As Integer
 
    ' Get a list of files it contains.
    Set files = New Collection
    file_name = Dir$(Dir_Name & "\*.*", vbReadOnly + _
                                        vbHidden + vbSystem + vbDirectory)
    Do While Len(file_name) > 0
        If (file_name <> "..") And (file_name <> ".") Then
            files.Add Dir_Name & "\" & file_name
        End If
        file_name = Dir$()
    Loop
 
    'Delete the files.
    For i = 1 To files.Count
        file_name = files(i)
        'See if it is a directory.
        If GetAttr(file_name) And vbDirectory Then
            'It is a directory. Delete it.
            fnDeleteDirectory file_name
        Else
            'It's a file. Delete it.
            SetAttr file_name, vbNormal
            Kill file_name
        End If
    Next i
 
    'The directory is now empty. Delete it.
    RmDir Dir_Name
End Function
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.