VBS to clean up old files or folders
'You can also use the archiver script if you don't want to delete them: [[VBS Script to Archive Files]]
'
'==Recursive Sub-Folder Version (the good one)==
'No idea why this was not here to begin with...
'==========================================================================
' Script to delete files that are older (based on modified date) than a particular date
' Kevin Guyer - 9/2010
'
' Set to run against the folder ''
' And permanently delete files older than 30 days based on modified date
'
' This can be customized further by changing the path defined in the variable 'filePath'
' and by changing the number of days in the variable 'timePeriodInDays'
'==========================================================================
OPTION EXPLICIT
DIM filePath, timePeriodInDays, fullQualifiedPath
' :: Define your file path to keep clean and age in days to retain (based on modified date, not created date)
filePath = "C:\temp\test"
timePeriodInDays = 31
' :: Kick off process
WalkAndDeleteFromTree filePath
'':: DEBUG :: ' MsgBox("Script Complete")
FUNCTION WalkAndDeleteFromTree(path) ' Recursive Function to process subfolders
ON ERROR RESUME NEXT
ERR.CLEAR
DIM fs, folder, file, item, childFolder
SET fs = CreateObject("Scripting.FileSystemObject")
SET folder = fs.GetFolder(path)
IF err.number <> 0 THEN ' This catches and allows you to handle and move on
'we have a failure, handle as desired
'':: DEBUG :: ' MsgBox("Error #: " + Err.Number + " - Desc: " + Err.Description)
ELSE
FOR EACH item in folder.Files
'':: DEBUG :: ' MsgBox("Looking at file: " & item.Name + ", with modified date of " & item.DateLastModified)
IF DateDiff("d", item.DateLastModified,Now) > timePeriodInDays THEN
item.Delete TRUE
End If
NEXT
END IF ' [ err.number <> 0 THEN ]
FOR EACH childFolder IN folder.SubFolders
' SubFolder? Process that folder's contents before moving on...
'':: DEBUG :: ' MsgBox("Found Subfolder")
WalkAndDeleteFromTree childFolder.Path
NEXT
END FUNCTION
'This will delete files or folders based on date of last modified:
==Files==
Files:
'==========================================================================
' Script to delete files that are older than a particular date
' Kevin Guyer - 200X
'
' This can be customized by changing the path defined in the variable 'filePath'
' and by changing the number of days in the IF statement
'==========================================================================
' :: OE to prevent the other, more humiliating OE
Option Explicit
' :: Declare Variables Holmes
Dim fso, oFolder, oFile, filePath, timePeriodInDays
' :: Define your file path to keep clean
filePath = "D:\output_archive"
timePeriodInDays = 14
' :: Open the path and folder
Set fso = createobject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(filePath)
' :: Walk each file and delete those with a DateCreated attribute that is of sufficient time past
For Each oFile In oFolder.files
If DateDiff("d", oFile.DateCreated,Now) > timePeriodInDays Then
oFile.Delete True
End If
Next
' :: Clean up
Set oFolder = Nothing
Set fso = Nothing
Set oFile = Nothing
==SubFolders==
Want to do subfolders instead of Files? Use this:
'==========================================================================
' Script to delete import data files that are older than a week
' Kevin Guyer - 2007
'
' This can be customized by changing the path defined in the variable 'filePath'
' and by changing the number of days in the IF statement
'==========================================================================
' :: OE to prevent the other, more humiliating OE
Option Explicit
' :: Declare Variables Holmes
Dim fso, oFolder, oFile, filePath, timePeriodInDays
' :: Define your file path to keep clean
filePath = "D:\Import\Daily\Archive"
timePeriodInDays = 14
' :: Open the path and folder
Set fso = createobject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(filePath)
' :: Walk each file and delete those with a DateCreated attribute that is of sufficient time past
For Each oFile In oFolder.subFolders
If DateDiff("d", oFile.DateCreated,Now) > timePeriodInDays Then
oFile.Delete True
End If
Next
' :: Clean up
Set oFolder = Nothing
Set fso = Nothing
Set oFile = Nothing