Script Center > Gallery > Storage > Delete All Files in a Folder That Have Not Been Modified in X Number of Days
TechNet Script Center logo

Welcome to the TechNet Script Center Gallery!

Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.

Delete All Files in a Folder That Have Not Been Modified in X Number of Days

(Community)
Rate it:
 
 
 
 
 
Script Code
VBScript
'===================================================================================
' Filename: PPQDelete.vbs
' Author: Eric Lee
' Company: BC Liquor Distribution Branch
' Date: January 5, 2006
'
' This file will check the timestamp of files in PestPatrol's quarantine directory
' and delete them if they were last modified more than 90 days ago.
'
'===================================================================================



'===================================================================================
' Declare variables
'===================================================================================
On Error Resume Next

Dim strPPQPath, strRawDateLM, strCleanDateLM, strTodayCDate, intTodayJDate, 
Dim strFileCDate, intFileJDate, intPurgeAge, strFileName

intPurgeAge = 90 'days
strPPQPath = "\\Program Files\\Common Files\\PestPatrol\\Quarantine\\"


'===================================================================================
' Set today's Julian date
'===================================================================================
' Convert Date format to variant
strTodayCDate = CDate(Int(Now))

' Perform conversion to Julian date
intTodayJDate = Int(CDbl(CDate(strTodayCDate))) + 15018


'===================================================================================
' Start checking files!
'===================================================================================
' ----------------------------------------------------------------------------------
' Instantiate WMI
' ----------------------------------------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService. _
    ExecQuery("Select * from CIM_DataFile where Path = '" & strPPQPath & "'")

' ----------------------------------------------------------------------------------
' Instantiate FileSystemObject for file deletion
' ----------------------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")


' Check file properties in each file in the folder above. Open loop.
For Each objFile in colFiles

' Set variable to working file name
strFileName = objFile.Name

' ----------------------------------------------------------------------------------
' Clean up Microsoft file date format to MM/DD/YYYY hh:mm:ss from YYYYMMDDhhmmss
' ----------------------------------------------------------------------------------
' Set temporary variable
strRawDateLM = objFile.LastModified

'Perform Date cleanup
strCleanDateLM = DateSerial(left(strRawDateLM,4),mid(strRawDateLM,5,2),mid(strRawDateLM,7,2)) + _
TimeSerial(mid(strRawDateLM,9,2),mid(strRawDateLM,11,2),mid(strRawDateLM,13,2))

' ----------------------------------------------------------------------------------
' Start converting Last Modified date of file to Julian date format
' ----------------------------------------------------------------------------------
' Convert Date format to variant
strFileCDate = CDate(Int(strCleanDateLM))

' Perform conversion to Julian date
intFileJDate = Int(CDbl(CDate(strFileCDate))) + 15018

' ----------------------------------------------------------------------------------
' Is file more than x number of days old?
' ----------------------------------------------------------------------------------
If intTodayJDate - intFileJDate > intPurgeAge Then
   ' If so, delete the filename in question
   objFSO.DeleteFile(strFileName)
End If

' Close loop, move to next file
strFileName = ""
Next
Platforms
Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 No
Windows 7 No
Windows Vista No
Windows XP No
Windows 2000 No
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.