Skip to content

Instantly share code, notes, and snippets.

@tekguy
Last active December 18, 2015 11:39
Show Gist options
  • Save tekguy/5776885 to your computer and use it in GitHub Desktop.
Save tekguy/5776885 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment