02 September 2014

Automate the Delete or Purge of old files using a simple script

If you have ever had the need to automate deleting of files older than a certain amount of days, this will help you out.

As an example IIS has a few logging options but unfortunately there is no way to purge off old records.
This script can be scheduled as a Windows Task and take care of that for you.

Just Specify the folder and the DateDiff
Save the files as logpurge.vbs
And specify that to run in Windows Task Scheduler
Set filesys = CreateObject("Scripting.FileSystemObject")
Set Directory = filesys.GetFolder("D:\LogFiles\IIS\W3SVC1")
Set Files = Directory.Files
For Each Modified in Files
If DateDiff("m", Modified.DateLastModified, Now) > 1 Then modified.delete
Next
The "m" refers to month, you can also specify week, day, year etc.

http://www.w3schools.com/vbscript/func_datediff.asp



No comments:

Post a Comment