Home > How-To Library > File/Folders Handling

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

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
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

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.