Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete folder and not just subfolders

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi I have a script which deletes the files and folders within subfolders of a folder. I need to delete all folders below objFolders (C:\scripts). Here it is:

strFolder = "C:\scripts"
intDays = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolders = objFSO.GetFolder(strFolder)

Recurse objFolders

Sub recurse(ByRef objFolders)
Set objSubFolders = objFolders.SubFolders
Set objFiles = objFolders.Files

For Each File In objFiles
If File.DateLastModified < objPastDate Then
On Error Resume Next
File.Delete
End If
Next

For Each Folder In objSubFolders
recurse Folder
Next

Set objSubFolders = Nothing
Set objFiles = Nothing
end sub

Thanks
 
You forgot to delete the folder after the recursive delete.
Code:
    For Each Folder In objSubFolders
        recurse Folder
        [red]Folder.Delete[/red]
    Next

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top