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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search folders and delete subfolders older than X days

Status
Not open for further replies.

Ted3496

IS-IT--Management
Jul 10, 2012
5
US
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
 
In fact you don't need a recursive procedure:
Code:
Const strPath = "\\servername\terminatedusers"
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objDept, objUser
For Each objDept In objFSO.GetFolder(strPath).SubFolders
  For Each objUser In objDept SubFolders
    If objUser.Datecreated < (Now() - 1095) Then
      WScript.Echo "Deleted Folder: " & objUser.Name
      objUser.Delete True
    End If
  Next
Next
WScript.Echo "Done."

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Wow, much simpler than I was trying to make it! Thanks! Just had to correct "For Each objUser In objDept SubFolder" to "For Each objUser In objDept.SubFolder
 
So as it often does, the requirements for this script have been changed. Now instead of looking at the created date of the user folder, I need to actually look inside of the user folder, find a file called info.txt, look at the created date of just that file, and then delete the folder it resides in based on the how old that file is. Seems like it would be really easy to do, but I can't figure it out. This is what I have right now that deletes the user folder based on the creation date

Code:
Const strPath = "c:\deletetest"
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")


Set objLogfile = CreateObject("Scripting.FileSystemObject")
Set objLog = objLogfile.OpenTextFile("c:\deletetest\log.txt", 8, True) ' 8=append, 2=overwrite

Call Search (strPath)
WScript.Echo "Done."
objlog.close

Sub Search(str)
Dim objDept, objUser
For Each objDept In objFSO.GetFolder(strPath).SubFolders
  For Each objUser In objDept.SubFolders
    If objUser.Datecreated < (Now() - 1095) Then
      objUser.Delete True
      objLog.Writeline(Now() & "  Deleted user folder: " & objUser.Name)
      
    End If
  Next
Next
End Sub
 
An update to my post above. So I've got it working now where it finds the info.txt file, and can delete (right now just having it echo it out so I can see it), but it just stops at the first folder it finds with that info.txt file instead of checking them all. How do I get it to keep down the folder structure? I'm sure it's something to do with where I set the path, etc, but I can't figure it out.

Code:
Const strPath = "c:\deletetest"
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const strInfo = "info.txt"

Set objLogfile = CreateObject("Scripting.FileSystemObject")
Set objLog = objLogfile.OpenTextFile("c:\deletetest\log.txt", 8, True) ' 8=append, 2=overwrite

Call Search (strPath)
WScript.Echo "Done."
objlog.close

Sub Search(str)
Dim objDept, objUser
For Each objDept In objFSO.GetFolder(strPath).SubFolders
  For Each objUser In objDept.SubFolders
     infoPath = objDept & "\" & objUser.Name & "\" & strInfo
     Set infoFile = objFSO.GetFile(infoPath) 
     If infoFile.DateCreated < (Now() - 1095) Then
       wscript.echo infoPath
      'objUser.Delete True
      'objLog.Writeline(Now() & "  Deleted user folder: " & objUser.Name)
    End If
  Next
Next
End Sub
 
Replace this:
If objUser.Datecreated < (Now() - 1095) Then
with this:
If objFSO.GetFile(objUser.Path & "\info.txt").Datecreated < (Now() - 1095) Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ugh, was it really that easy???? Why do I make this stuff so complicated? Thank you!
 
Hi Friends,

I am loooking for a Vbscript which will delete only subfolders starting with the name older than x days.

Example:

c:\abc\user1\temp\_SAxxx - I want to remove xxx folders _SA
C:\abc\user2\temp\_SAxxxx
c:\abc\user3\temp\_saxxx

I have almost 80 users folder under c:\abc.


Please help

Thanks,
Sandy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top