I need to find a way to delete everything after the third .bak file in the SQL Server backup folder

Working with an old SQL Server cluster with limited space available. The cluster uses a database of SAS (server database 2003) for data backup: fc_mdt

, fc_mdt_trend

, fc_adt

. Inside these folders are several files .bak

and .trn

.

I am looking for a way to automate deleting everything after the third file .bak

when the contents of the folder are specified by the date modified

+3


source to share


2 answers


If you are ok with powershell then this script will help you



Get-ChildItem "C:\YourDirectory" | where{$_.Extension -eq ".bak"} |
sort -Property LastWriteTime -Descending| Select -Skip 3 | Remove-Item -Include .bak

      

+3


source


If you are performing automatic backups using a maintenance plan, why not configure it to delete files older than a certain age, for example:



enter image description here

0


source







All Articles