Friday, February 12, 2010

Delete files older than certain number of days using a windows batch file

Same old problem keeps repeating at every client place! The customized backup jobs sometimes  fail to clean up the files older than 2 days (or whatever the threshold is) and the backup jobs fail eventually when the drive gets full, One smart and quick solution for this is to use the 'forfiles' utility and schedule it as a task in windows or use xp_cmdshell to schedule the same as a SQL server agent job. The command looks something like the below:

forfiles /p D:\backup\ /s /m *.trn /d -2 /c "cmd /c del @file"

Change the extension from .trn to .bak if you  are trying to cleanup full backup files instead of transaction log files.

No comments:

Post a Comment

Failed to obtain the resource handle for cluster resource with name or ID 'xxxxxxxxxxxxxxx' error on Availability Group databases

We recently had failures for our Backup Maintenance Plan (performing T-Log backups) and it would fail with the below error message: Message ...