How do I delete files on my Windows PC or server older than X days?

Fortunately Windows has a few command line “tricks” that you can do to achieve things like automatically deleting files older than X days.

The handy command is forfiles. The example below will delete all files in c:\backups that are older than 7 days.

forfiles /s /p c:\backups /m *.* -d -7 /c “cmd /c del @file”

You can adjust it accordingly to change the path (/p flag) or the file name (/m flag).

If you need to use this on a frequent basis, you can schedule your .bat file to run via Task Scheduler.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top