I have a macro in excel that runs before save and creates a backup of an excel table with the actual date in its name.
These backups started to take too much space, so I have inserted another macro that deletes backups older than 14 days. The problem is that sometimes we don't save new copies for 2 weeks or months, so I need a macro that will leave only the 5 newest backups and delete the rest.
The current macro used:
'======================================================================================
'delete old backup
Set fso = CreateObject("Scripting.FileSystemObject")
For Each fcount In fso.GetFolder(ThisWorkbook.Path & "\" & "excel_backups" & "\").Files
If DateDiff("d", fcount.DateCreated, Now()) > 14 Then
Kill fcount
End If
Next fcount
'======================================================================================
backups are saved in this format:
ThisWorkbook.Path & "\excel_backups" & "\backup_" & Format(Date, "yyyy.mm.dd") & ".h" & Hour(Now) & "_" & ActiveWorkbook.name
so a backup looks like this: backup_2014.12.18.h14_[filename].xlsm
My question is: can this be modified somehow to delete only the oldest ones, and leave the last 5 newest of them? I have no idea how to start writing that.
Thank you for your time.