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).
Steps:
1. download and install Windows Server 2003 Resource Kit Tools
2. write a bat script
3. create a scheduler
Let look at the details
1. download and install Windows Server 2003 Resource Kit ToolsIn 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:\WINDOWS\system32\forfiles.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:\WINDOWS\system32\LogFiles\W3SVC1
- 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:\WINDOWS\system32\LogFiles\W3SVC1 -s -m *.log -d -14 -c "Cmd /C DEL @File"
copy forfiles.exe from C:\WINDOWS\system32\forfiles.exe and managelog.bat to the same location which you prefer.
3. create a schedulerYou 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:\WINDOWS\system32\LogFiles\W3SVC1 -s -m *.log -d -14 -c "Cmd /C ECHO @File"
Done. Good luck.