Log files are very useful but making sure that they don’t overrun your system can be cumbersome. Unless the logging system supports and is configured for circular logging, unmonitored log files can eat up valuable disk space quickly. There is a simple way to keep log files from consuming large portions of your disk: by using the ‘forfiles‘ command and a scheduled task to automatically delete old log files.

Open a command prompt and type forfiles /? to see the switch options that are available. For this exercise we will use the date, pathname, recursive and command switches.

In our example below, we will delete files older than 3 years (1095 days) located in the c:\mysoftware\logs folder. The command syntax is as follows:

forfiles /p “c:\mysoftware\logs” /s /d  -1095 /c “cmd /c del @file”

Open notepad and enter the above command, replacing c:\mysoftware\logs with your own log folder and the -1095 with your own deletion time (in days). Save the file and then change the .txt extension to a .bat extension so that it can be run as a batch file ( see image shown below).

Next, open Task Scheduler and create a new task as shown below.

Schedule the frequency and add the BAT file as the program/script to be executed. Your script will run automatically and delete files older than the configured days.

 

 

Leave a comment

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

error: Sorry, copy/paste is disabled
Skip to content