Monday, 7 November 2011

Delete files by age using FORFILES

Ever needed to tidy up some old backups? Perhaps wanted to delete a bunch of files by age?

If you're using standard SQL tools for local backups and old file tidy ups you can get into the situation I just faced when the files are not tidied up in time. Your disk fills up, your backup and cleanup task starts failing and the world comes crashing down.

Sure you can manually delete old files, but when there's lots of files in lots of directories and no PowerShell you'll want something to help you. Enter FORFILES.EXE

I ran the following to tidy up files older than 2 days in the backup directory:

forfiles /m *.* /s /d -2 /c "cmd /c del @path"

That command will delete (/c "cmd /c del @path") all files (/m *.*) older than 2 days (/d -2) in the current folder and all its sub-directories (/s).

Update (2012-02-07)

I recommend you check out PowerShell for all your command line needs. I blogged about a PowerShell way to do this here:

No comments:

Post a Comment