简体   繁体   中英

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

Working on an old SQL Server cluster that has a limited amount of space available. The cluster uses a SAS (server 2003 database) for data backups: fc_mdt , fc_mdt_trend , fc_adt . Inside those folders are multiple .bak and .trn files.

I'm looking for a way to automate deletion of everything after the third .bak file when the folder contents are listed by date modified

If you are ok with using 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

If you're performing automated backups using a maintenance plan why not configure it to remove files older than a specific age, for example:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM