2

Can somebody tell me how I write a batch file that I can attach to a scheduled task that will delete IIS logs that are older than 2 weeks.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Tarik
  • 79,711
  • 83
  • 236
  • 349
  • 1
    Which part of this are you having trouble with, specifically? – Oliver Charlesworth Aug 30 '11 at 20:24
  • This should help. http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days – Vamsi Emani Aug 30 '11 at 20:25
  • If you mean actually writing the file, just use notepad or any other text editor and save it as a .bat or rename it once you're done. If you need the content, see VonC's post below. – Kevin Aug 30 '11 at 20:27
  • 1
    Be very careful of unintended behavior when designing anything like this. For example, make sure that it can only operate on the directory and files that you intend it to clean. I once had a system upgrade go very badly when someone added his favorite "log file cleaner" script to the crontab (it was Unix) just as we headed out for dinner. The next morning, the "cd" command that was supposed to cd to the user's personal temp directory failed because that temp directory didn't exist yet. So it started recursively removing files older than 2 weeks, starting at the root. It was a long day. – Chris Thornton Aug 30 '11 at 21:21
  • @Chris : Thanks for the warning Chris. – Tarik Aug 30 '11 at 21:26

3 Answers3

8

Use forFiles in a batch file:

forfiles -p C:\WINDOWS\system32\LogFiles\W3SVC1 -s -m *.log -d -14 -c "Cmd /C DEL @File"

Commandline params explained:

-d -14                                   //  Specifies num days (14 for 2 weeks)
-p C:\WINDOWS\system32\LogFiles\W3SVC1   // path
-m *.log                                 // file mask

You can find all in the technet link in my post.

You will need to install one the Win server resource kits such as Windows Server 2003 Resource Kit Tools

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • This is not working under Windows XP. – Tarik Aug 30 '11 at 20:40
  • @Braveyard: See my edit. You need the resource kit. Can be Win2k3 or later. – Mrchief Aug 30 '11 at 20:41
  • If you don't have forfiles installed by default on your machine, get it from [Microsoft FTP server](ftp://ftp.microsoft.com/ResKit/y2kfix/x86/). Place it to C:\WINDOWS\system32\forfiles.exe – smoak Aug 30 '11 at 20:49
  • Can you also write down the one which accepts day and path as parameters. Thanks a lot. – Tarik Aug 30 '11 at 20:52
  • Edited my answer. Do check the technet link which explains all params in detail. – Mrchief Aug 30 '11 at 20:56
  • This is what I wrote and can you check whether it is correct? : `set path=%1 set age=%2 forfiles -p %path% -s -m *.log -d -%age% -c "Cmd /C DEL @File"` – Tarik Aug 30 '11 at 20:57
1

You could use the script deleteoldfiles.bat as an example.
(From "How to Delete Old IIS Log Files" blog post)

The following example deletes all files named “ex*.log” that were last modified over 1 year (365 days) ago. The starting directory is C:\Windows\System32\LogFiles:

deleteoldfiles.bat C:\Windows\System32\LogFiles ex*.log 365

The following example searches all subdirectories in C:\Windows and deletes all files named “dump.txt” that were last modified over 60 days ago:

deleteoldfiles.bat C:\Windows dump.txt 60

From there, you can schedule that batch, making sure of the arguments and their enclosing quotes aren't an issue.
See for instance "How to use the Windows Task Scheduler".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"

Syntax FORFILES [/p Path] [/m Mask] [/s] [/c Command] [/d [+ | -] {dd/MM/yyyy | dd}]

Key /p Path The Path to search (default=current folder)

/s Recurse into sub-folders

/C command The command to execute for each file. Wrap the command string in double quotes. Default = "cmd /c echo @file"

            The Command variables listed below can also be used in the
            command string.

/D date Select files with a last modified date greater than or

Luis
  • 1,040
  • 1
  • 14
  • 22