Search This Blog

Tuesday, April 15, 2008

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).

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 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:\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
  1. log file location = C:\WINDOWS\system32\LogFiles\W3SVC1
  2. file type = *.log
  3. 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 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:\WINDOWS\system32\LogFiles\W3SVC1 -s -m *.log -d -14 -c "Cmd /C ECHO @File"

Done. Good luck.

4 comments:

Anonymous said...

I'm doing something very similar but I'd like to add a log that records the filenames that were deleted and any errors that may have occured such as:
ERROR: No files found with the specified search criteria.
If I redirect the output of the command to file.log I get most of the output but not the errors. Here's what I have:

forfiles /p "Z:\test\" /d -1 /C "cmd /C echo @file." >> file.log

If no files are found that match it prints
ERROR: No files found ...
but that does not get recorded in the logfile.

Any suggestions?

Thanks,
Jake

amar/eBeku.com said...

-there's something called output redirection, try that.

try adding the below line
2>&1
after the log file name to make it look like

forfiles /p "Z:\test\" /d -1 /C "cmd /C echo @file." >> file.log 2>&1

Anonymous said...

Thanks for 2>&1

Joshua said...

Hey thanks for the info, I'll be using this to manage IIS files in my environ here at work.