Submitted By: Eric Lee
Language: VBScript
Description: Checks the timestamp of files in any directory on the default drive (PestPatrol's quarantine directory, in this example) and deletes those files if they were last modified more than 90 days ago.
Script Code
'===================================================================================
' 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
Note: Not all scripts run on all versions of Windows.
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts or on the Script Center, please contact scripter@microsoft.com.
Disclaimer
This script is not supported under any Microsoft standard support program or service. The script is 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 script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script 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 script or documentation, even if Microsoft has been advised of the possibility of such damages.