'http://www.theswamp.org/index.php?topic=26167.0
Function deletefilesandfolders (foldername, deep)
dim fso,fldr
set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder(foldername)
MsgBox "The folder " & fldr & " exists so now to delete all folders inside.", vbInformation
intAnswer = Msgbox("Do you want to delete these files", vbYesNo, "Delete Files")
If intAnswer = vbNo Then
Exit Function
End If
deep = UBound(Split (foldername, "\")) + deep
recurse fldr, deep
Set fso = Nothing
Set fldr = Nothing
End Function
Sub recurse(byref fldr, deep)
dim subfolders,files,folder,file
set subfolders = fldr.subfolders
set files = fldr.files
for each file in files
on error resume next
name = file.name
file.Delete True
If Err Then
MsgBox "Error deleting:" & Name & " - " & Err.Description, vbInformation
Else
MsgBox "Deleted:" & Name, vbInformation
End If
On Error GoTo 0
next
for each folder in subfolders
recurse folder, deep
If UBound(Split (folder.path, "\")) > deep Then
name = folder.name
folder.Delete True
If Err Then
MsgBox "Error deleting:" & Name & " - " & Err.Description, vbInformation
Else
MsgBox "Deleted:" & Name, vbInformation
End If
On Error GoTo 0
End If
next
set subfolders = nothing
set files = nothing
End Sub
deletefilesandfolders "C:\NewTemp", 0
'0==delete all subfolders . 1==leave subfolders 1 deep . 2==leave subfolders 2 deep...and so on...