TheTAZZone - Internet Chaos

Recursive file pruning…

ORIGINALLY POSTED BY TALEN FOR THETAZZONE/TAZFORUM HERE

NOT ALL THE CODE MAY BE IN THIS POST…PLEASE FOLLOW THE LINK TO THE ORIGINAL THREAD

If you’ve ever had a need to prune files based upon their age, this VBScript will start in an explicitly defined directory and recurse through all subdirectories therein. Recommend that you pipe the output to a text file for review prior to removing the echo statements and modifying the script to actually delete files.

Code: Select all
'   NAME: recursiveFilePrune.VBS
'   AUTHOR: Talen
'   DATE  : 2007.03.12
'   COMMENT: This script is designed to prune files based upon age. It will
'      start in an explicitly defined directory and recurse through any and all
'      subdirectories below the starting directory. It reads the CreationDate
'      property of the file itself and does not discriminate by file type.
'###############################################################################
Option Explicit

Dim strPruneDate, strDay, strMonth, strYear, strTargetDate, strDate
Dim strComputer, strDrive, strPath, strPathAlt, strName
Dim objWMIService
Dim colFiles, objFile
Dim colFolders, objFolder
Dim strFolderPath, strFilePath
Dim colSubFolders, objSubFolder
Dim strSubFolderPath, strSubFilePath
Dim colSubFiles, objSubFile
‘###############################################################################
‘# The strPruneDate variable holds the string used to determine how old of a   #
‘# file will be pruned. In this example, the script will prune files older     #
‘# than six months (the ‘m’ in quotes tells the script to use months and the   #
‘# ‘-6’ tells the script to add negative six (months) to the current system    #
‘# date). Google ‘vbscript DateAdd function’ for more information.             #
‘###############################################################################
strPruneDate = DateAdd(“m”, -6, Date)
‘###############################################################################
‘# The strDrive and strPath variables should be fairly straight forward. This  #
‘# where the starting directory is explicitly defined. The only caveat being   #
‘# if you have backslash in your path, you need to use two when defining       #
‘# strPath (as shown in the example below). Do not include the leading back-   #
‘# slash, as the script will provide it.                                       #
‘###############################################################################
strDrive = “C:”
strPath = “Documents and Settings\\Talen\\My Documents”
‘###############################################################################
‘#                        !!!*D*I*S*C*L*A*I*M*E*R*!!!                          #
‘# Be *very* certain you know what is in the directory, it would not do to run #
‘# this script, for example, in C:\Windows. You have been warned…            #
‘###############################################################################
strPathAlt = Replace(strPath, “\\”, “\”)
strName = strDrive & “\” & strPathAlt

strDay = Day(strPruneDate)
If Len(strDay) < 2 Then
strDay = “0” & strDay
End If
strMonth = Month(strPruneDate)
If Len(strMonth) < 2 Then
strMonth = “0” & strMonth
End If
strYear = Year(strPruneDate)
strTargetDate = strYear & strMonth & strDay

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer)
Set colFiles = objWMIService.ExecQuery _
(“SELECT * FROM CIM_DataFile WHERE Drive = ‘” & strDrive & _
“‘ AND Path = ‘\\” & strPath & “\\'”)
Set colFolders = objWMIService.ExecQuery _
(“ASSOCIATORS OF {Win32_Directory.Name='” & strName & “‘} ” _
& “WHERE AssocClass = Win32_Subdirectory ” _
& “ResultRole = PartComponent”)
‘###############################################################################
WScript.Echo “Checking for all files created on or before ” & strTargetDate & _
” in:” & vbCrLF & vbTab & strName

For Each objFile in colFiles
FileCreationDateCheck
Next
‘——————————————————————————-
For Each objFolder in colFolders
WScript.Echo vbCrLF & “Checking for all files created on or before ” _
& strTargetDate & ” in:” & vbCrLF & vbTab & objFolder.Name

strFolderPath = Replace(objFolder.Path, “\”, “\\”)
strFilePath = strFolderPath & objFolder.FileName & “\\”
Set colFiles = objWMIService.ExecQuery _
(“SELECT * FROM CIM_DataFile WHERE Drive = ‘” & strDrive & _
“‘ AND Path = ‘” & strFilePath & “‘”)

For Each objFile in colFiles
FileCreationDateCheck
Next
ShowSubFolders objFolder.Name
Next
‘——————————————————————————-
Sub FileCreationDateCheck
strDate = Left(objFile.CreationDate, 30)
If strDate < strTargetDate Then
Wscript.Echo objFile.Name
‘ Replace w/ objFile.Delete after testing
End If
End Sub
‘——————————————————————————-
Sub ShowSubFolders(strSubFolder)
Set colSubFolders = objWMIService.ExecQuery _
(“ASSOCIATORS OF {Win32_Directory.Name='” & strSubFolder & “‘} ” _
& “WHERE AssocClass = Win32_Subdirectory ” _
& “ResultRole = PartComponent”)

For Each objSubFolder in colSubFolders
Wscript.Echo vbCrLF & “Checking for all files created on or before ” _
& strTargetDate & ” in:” & vbCrLF & vbTab & objSubFolder.Name

strSubFolderPath = Replace(objSubFolder.Path, “\”, “\\”)
strSubFilePath = strSubFolderPath & objSubFolder.FileName & “\\”
Set colSubFiles = objWMIService.ExecQuery _
(“SELECT * FROM CIM_DataFile WHERE Drive = ‘” & strDrive & _
“‘ AND Path = ‘” & strSubFilePath & “‘”)

For Each objSubFile in colSubFiles
strDate = Left(objSubFile.CreationDate, 30)
If strDate < strTargetDate Then
Wscript.Echo objSubFile.Name
‘ Replace w/ objFile.Delete after testing
End If
Next
ShowSubFolders objSubFolder.Name
Next
End Sub

Enjoy!

One Response to Recursive file pruning…

  1. Eugene October 20, 2008 at 10:18 pm

    looking forward for more information about this. thanks for sharing. Eugene

Leave a Reply

Your email address will not be published. Required fields are marked *

Advertise

If you'd like to advertise on The Mutt ( aka TheTAZZone.com ) feel free to contact us at: administration[at]thetazzone.com

TheTAZZone is a non-commercial entity. We do not sell any products or services ourselves. Our revenue comes from advertising and donations only.

We appreciate your support! Your advertising revenue ( or donations ) helps us to continue to upgrade, improve, and offset the costs of maintaining this site.

Donations can be made through the page ' Donate '.