I'm working on a script that will scan a folder with a bunch of subfolders, and then delete the subfolders of those folders than are older than X days. Confusing I know. Basically we have a folder where we store terminated user data named \\servername\terminatedusers. Inside that folder are a bunch of subfolders named for the department the user was in. Then each of those department folders contains the actual user folders. What I'd like to do is look inside each department folder and check the date of each user folder. If the user folder is older than say 3 years, delete it. Right now I have the script below which works just fine for deleting department subfolders of \\servername\terminatedusers older than 3 years, but unfortunately that becomes a bit of a problem when the user folders contained within are newer than 3 years.
Code:
Const strPath = "\\servername\terminatedusers"
Dim objFSO
Dim strFoldername
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call Search (strPath)
WScript.Echo"Done."
Sub Search(str)
Dim objFolder, objSubFolder, objFile
Set objFolder = objFSO.GetFolder(str)
For Each objSubFolder In objFolder.SubFolders
If objSubFolder.Datecreated < (Now() - 1095) Then
strFolderName = objSubFolder.name
wscript.echo "Deleted Folder:" strFolderName
objSubFolder.Delete(True)
End If
Next
For Each objSubFolder In objFolder.SubFolders
Search(objSubFolder.Path)
Next
End Sub