Just Code : Delete all files with specific month

I needed a quick bit of code today so that i could clean up my hard disk. This script will delete all the files that match a specific month number. One day it may save you a few minutes.

LoopFolders("D:\Servers\116\SQLBackups",10)

Sub LoopFolders(sFullPath,iMonth)
Dim oFolder,oSubFolder,oFSO,oFile

Set oFSO = CreateObject("Scripting.FileSystemObject")

If oFSO.FolderExists(sFullPath) Then
Set oFolder = oFSO.GetFolder(sFullPath)

For Each oFile In oFolder.Files
if ( Month(oFile.DateCreated) = iMonth ) Then
debug.Log(oFolder.Path + "\" + oFile.Name +":" + ctext(oFile.DateCreated))
oFSO.DeleteFile(oFolder.Path + "\" + oFile.Name)
End If
Next

For each oSubFolder in oFolder.SubFolders
debug.Log("“Folder:"+ oSubFolder.Name)
LoopFolders(oSubFolder.Path,iMonth)
Next

End If

Set oFSO = null
Set oFolder = null
Set oFile = null
End Sub

Leave a Comment

%d bloggers like this: