Some application don’t have the ability to delete old archive /log files , they can create the archive /log file but it can’t delete the Old one , due to this system administrator will face the problem of no more space and disk is fill massage, where he need to delete the old archive /log files manually.
To Over come this case you can create a script and run it on daily or weekly bases using scheduling task from the control panel below is such a script that can run under windows Xp/2000/2003, in the script we set the number of days we need to key their log if you need to keep the last 10 day archive files set this value to 10 , we also have a condition that check the number of files in the folder (45) , so that if there is a problem in the archiving process and we have file older that 10 days we don’t to delete them.
We then compare the creation date of the archive file against our target date. You can specify the extension of the archive / log file to avoid deleting other files in the folder in case there are such files
REM set number of last days to keep
Keeplastndays=10
REM Get today date minus the number of days to keep thier
dtmDate = Date - Keeplastndays
REM get the day value
strDay = Day(dtmDate)
Rem if day value is 1 digit add leading 0
If Len(strDay) < 2 Then
strDay = "0" & strDay
End If
REM get the Month value
strMonth = Month(dtmDate)
Rem if month value is 1 digit add leading 0
If Len(strMonth) < 2 Then
strMonth = "0" & strMonth
End If
REM get the year value
strYear = Year(dtmDate)
REM this the date for last date you need to keep
strTargetDate = strYear & strMonth & strDay
REM run against local computer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
REM get List of files at folder Name='F:\archivelog'
Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='F:\archivelog'} Where " _
& "ResultClass = CIM_DataFile")
Rem Get total number of files in the folder
CC=FileList.count
Rem go through each file
For Each objFile In FileList
Rem if there is less than 45 file in the folder the quit and do nothing
If CC < 45 then
Wscript.Quit
End If
strDate = Left(objFile.CreationDate, 8)
If strDate < strTargetDate Then
If objFile.Extension = "001" Then
objFile.delete
CC=CC-1
End If
End If
Next