Folder Delete Everything

|

Careful with this function as it can really do some serious damage if you choose the wrong folder.

 'Folder Delete Everything - Start
    Private Sub DeleteDirectory(ByVal dir_name As String)
        Try
            Dim file_name As String
            Dim files As Collection
            Dim i As Integer

            ' Get a list of files it contains.
            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.
                    DeleteDirectory(file_name)
                Else
                    ' It's a file. Delete it.
                    'lblStatus.Text = file_name
                    'lblStatus.Refresh()
                    SetAttr(file_name, vbNormal)
                    Kill(file_name)
                End If
            Next i

            ' The directory is now empty. Delete it.
            'lblStatus.Text = dir_name
            'lblStatus.Refresh()
            RmDir(dir_name)
        Catch ex As Exception
            'handle any errors that occurred
        End Try
    End Sub
    'Folder Delete Everything - Stop
Try
   DeleteDirectory("c:windows")
Catch ex As Exception
End Try
Originally Posted on December 14, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.