Deleting old files

Oftentimes, I like to backup SQL databases to a local folder, then include that folder in my backup to tape/disk/network. That way, the backup software doesn't have to be database aware; it's just backing up files.



SQL Server allows you to schedule backups and there's an option to clear out the old backup files, but I've found it doesn't always work quite right. I'd always struggled with a way to do this until I found the FORFILES command.



Now, I'm not sure what version of Windows this appeared it, but I know it's in Windows Server 2003.

forfiles /p e:\backup /m *.bak /d -2 /c "cmd /c del @file"

The /c option allows you to run any command, so I'm using the del command to delete all files older than a certain date. Certainly, you could do whatever you want, like move them or copy or print their attributes.

The /d is where you specify a date or a number of days. A plus gives you files after that date and a minus gives you files before that date. In my example, files more that two days old are selected to be deleted.

I never had a good way to do this before, so I was always resorting to a script written in another language or a customized batch file (and batch programming is not fun!)

Established 2005 · Databasically © 2016