Manage/Delete IIS log files using scheduler

Microsoft have provide a very good resource kits. Forfiles is one the them that we can use to manage our log files especially IIS log. My purpose for this article is to show how i use forfiles to help me to manage/delete my IIS log file more than 14 days (you can delete any time frame you want).



1. install Windows Server 2003 Resource Kit Tools

2. write a bat script

3. create a scheduler



Let look at the details


1. install Windows Server 2003 Resource Kit Tools


In order to us it, you need to install it in your server first. Go to microsoft website “Windows Server 2003 Resource Kit Tools” and download it. After finish downloading, you can start to install it.

After finish installation, you can find Forfiles in C:WINDOWSsystem32forfiles.exe



2. write a bat script


Syntax for forfiles


forfiles [/p Path] [/m SearchMask] [/s] [/c Command] [/d[{+ -}] [{MM/DD/YYYY DD}]]


the details syntax you can get it from technet.


For me, i want to delete the log files date 14 days before. Below are some of the details


log file location = C:WINDOWSsystem32LogFilesW3SVC1

file type = *.log

Date to delete = -d -14 (14 days before)


Below is the command that i’m able to come out and save it as .bat format. Say, “managelog.bat”


Forfiles -p C:WINDOWSsystem32LogFilesW3SVC1 -s -m *.log -d -14 -c “Cmd /C DEL @File” >> file.log 2>&1


copy forfiles.exe from C:WINDOWSsystem32forfiles.exe and managelog.bat to the same location which you prefer.


3. create a scheduler


You need to create a scheduler to run the job automatically. For me, i run my job weekly at mid night 12:30AM.


To know how to create a window scheduler job, please find reference here.


Please test it before make it run automatically. I suggest that you use below command line to test. I change the DEL to ECHO so that i work safely.


Forfiles -p C:WINDOWSsystem32LogFilesW3SVC1 -s -m *.log -d -14 -c “Cmd /C ECHO @File”  >> file.log 2>&1


Done. Good luck.