tekguy
6/13/2013 - 8:05 PM

VBScript to delete old .bak files from specified directory

VBScript to delete old .bak files from specified directory

'==============================
'Remove old backups
'http://gordondurgha.com
'=============================

'Definitions
iDaysOld = 3
strExtension = ".bak"
sDirectoryPath = "D:\MSSQL\Backups\"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files

'Walk through each file in this folder collection.
  For each oFile in oFileCollection
  If oFile.DateLastModified < (Date() - iDaysOld) Then
	      If Right(UCase(oFile.Name), Len(strExtension)) = UCase(strExtension) Then
	              oFile.Delete(True)
	      End If
	End If
  Next

'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing