Excel VBA - deleting populated folder problem

P

PaulC

I can delete an empty folder with RmDir, but how do I delete a populate
folder and all its sub-folders?

Pau
 
B

Bob Phillips

Hi Paul,

Here is some code

Dim sFile
Dim sPath As String

sPath = "C:\NewDir\myTest"

Do
sFile = Dir(sPath & "\*.*")
If sFile <> "" Then
Kill sPath & "\" & sFile
End If
Loop Until sFile = ""
RmDir sPath

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
D

Dana DeLouis

Just another way:

Function DeleteFolder(sFolder As String) As Boolean
'// Dana DeLouis
'// = = = = = = = = = =
'// Folder can have Wildcard Characters
'// True if folders with the read-only attribute set are to be deleted
'// = = = = = = = = = =
On Error Resume Next
With CreateObject("Scripting.FileSystemObject")
.DeleteFolder sFolder, True
DeleteFolder = Err.Number = 0
End With
End Function


Sub TestIt()
Dim Status As Boolean
Status = DeleteFolder("D:\Junk")
' Delete all Temp* Folders
Status = DeleteFolder("D:\Temp*")
End Sub

HTH
 
P

PaulC

Thanks Bob

Code works fine. Now I am working on extending it to all the subfolder
that I have. If I can't solve this I may get back to you.

Pau
 
B

Bob Phillips

Pleasure Paul.

Of course you can. As a starter, I would use FileSystemObject in a recursive
routine.

If you do post back, start a new thread as this one will die shortly.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top