VBScript clearner.vbs - Delete File Based on Last Modified Date and Filter by File Extension

A VB script, i forgot where i got it.

===

'******************************************************************
' naam: Cleanup.vbs
' function:
' - Cleanup old files depending on the age of x days
' - Check for file extension
' - Possible to recurse in subfolders
' - For clustered drives, will check first if the drive exists on that server
' author: SDR
'******************************************************************
Option Explicit
Dim wshShell
Dim FilePath, Retention, FileFilter, FilterExt, SubFilter
Dim fso, Folder, DriveLetter
Set wshShell = wscript.createobject("wscript.shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'*******************************************************************
' Fill in below variables
' ex.
' FilePath = "D:\BACKUP"
' Retention = 2
' note: Retention is in days
' If you need to filter by extention, set the FileFilter to True and fill in FilterExt
' If you need to also filter in the subfolders, set SubFilter to True
'*******************************************************************
FilePath = "D:\Test"
Retention = 5
FileFilter = True
FilterExt = "ARCHIVE"
SubFilter = True

'*******************************************************************
' Do not change below!
'*******************************************************************

DriveLetter = Left(FilePath, 1)

If fso.DriveExists(DriveLetter) Then
' Cleanup the files
DelFiles FilePath, Retention, FileFilter, SubFilter
End If

Sub DelFiles(Dir, days, DelFilter, SubFoldersFilter)
Dim DelDate, FSO, Folder, SubFolder, SubDir, Files, File, FileDate, Ext, FileDelete, ErrorDelete
Set FSO = createobject("scripting.filesystemobject")
On Error Resume Next
Set Folder = FSO.GetFolder(Dir)

DelDate = NOW - days
On Error Goto 0
Set Files = Folder.Files
Set SubFolder = Folder.SubFolders
For each File in Files
FileDate = File.DateLastModified
Ext = FSO.GetExtensionName(File)
On Error Resume Next
If DelFilter = True then
If FileDate <= DelDate and Ext = FilterExt then
File.Delete
End If
Else
If FileDate <= DelDate then
File.Delete
End If
End If
On Error Goto 0
Next
If SubFoldersFilter = True then
For each SubDir in SubFolder
DelFiles SubDir.Path, days, DelFilter, SubFoldersFilter
Next
End If
Set Files = Nothing
Set Folder = Nothing
Set FSO = Nothing
End Sub

===

//alak

Comments

Popular posts from this blog

Quick Dirty Script for Checking Oracle Data Gurad Gap and Emailing it

SAP SWPM Software Provision Manager SL Toolset 1.0 SP22 Startup Options

SAP SystemCopy - Activation of TCI Notes, DigiSIgn Gone Wrong