So if you don’t need these files, why keep them? We are going to show you how to easily remove these old log files to keep you system nice and tidy.

Of course, while the we are covering below are immediately useful for managing log files, you can also apply the same techniques to any other type of “expiring” file (such as backups).

Remove Files Based on Last Modified Date

If you want to clear your existing log files based solely on the last modified date of the file, all you have to do is use the FORFILES command. For example:

The above command would delete all files from the “C:LogFiles” folder, and all sub-folders which have not been modified in the last week.

The FORFILES command is pretty flexible with the search pattern and date functions. For example, in place of a number, you can enter a date such as ‘-1/13/2010’ to delete files last modified prior to the specified date.

To get all the details on what FORFILES can do, view the online help using the following command from the command prompt:

Remove Files Based on a Date Pattern in the File Name

Many applications and services produce log files based on a date pattern as to have one log file per day (i.e. Log100113.txt, Backup-2010-01-13.zip, etc.). For these types of files, it is preferable to delete based on the date of the file incorporated into the file name rather than the last modified date. This is useful for scenarios such as keeping all log files for the past 3 months. Unfortunately, Windows does not have a native command with this type of logic but with a batch script we can easily handle this task.

There are examples included in the usage comments on the script, so it should be pretty easy to figure out.

The Script

Automating the Process

The FORFILES command is native to Windows, however the DeleteByDatePattern script should be placed in a folder defined in your Path variable (such as your Windows folder) so it can be called as though it were a native command. Once this is done, you can create a scheduled task which is either a single command (if you only need to delete from a single location) or a batch file (if you need to delete from multiple locations) which runs daily, weekly, monthly or whenever.

One more thing you can set and forget.

Download Delete By Date Pattern script from Sysadmin Geek