Solved

Script to clear log files which are 15 days old.

Posted on 2011-09-04
4
276 Views
Last Modified: 2012-06-27
Hi,
Looking for script which will clear up log files which are older than 15/20 days on C drive with below path
c$\WINNT\system32\LogFiles
THis is to ensure that we do not run out of disk space on that drive.
Let me know if anyone has used such script.
Thanks.
0
Comment
Question by:ex_ad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Expert Comment

by:danmb
ID: 36480602
Well I found a script "VBScript to Delete Old Files" by Sidd at http://www.symantec.com/connect/downloads/vbscript-delete-old-files, and made some changes to it.

For safety's sake, because I don't want to be responsible for deleting files on your computer, please review the 3 lines that are commented with "Specify:" and make sure they have the
- correct directory path,
- correct age of files to delete, and
- correct file extension of the files to delete (no dot, just the extension).

Once you've done that, you must also manually uncomment the line that actually does the deletion.

'                   oFile.Delete(True)


' Based on "VBScript to Delete Old Files" by Sidd
' http://www.symantec.com/connect/downloads/vbscript-delete-old-files
' with some changes to the file extension code
'
' For safety sake, please review the 
' - Directory Path, 
' - Age of Files, and
' - Extension of Files to Delete 
' lines, and modify as necessary.
'
' You must also UNCOMMENT  
' oFile.Delete(True) 
' to actually do the deletion.
'

Option Explicit 
On Error Resume Next 
Dim oFSO, oFolder, sDirectoryPath 
Dim oFileCollection, oFile, sDir 
Dim iDaysOld, sExt


' *************************************************


' Specify Directory Path From Where You want to clear the old files 

sDirectoryPath = "c:\WINNT\system32\LogFiles" 

' Specify Age of Files to Delete, in Days

iDaysOld = 20

' Specify extension of files to delete (no ".", just the extension)

sExt = "log"


' *************************************************


sExt = LCase(sExt)

Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFSO.GetFolder(sDirectoryPath) 
Set oFileCollection = oFolder.Files 

For each oFile in oFileCollection
	If LCase(oFSO.GetExtensionName(oFile)) = sExt Then
 		If oFile.DateLastModified < (Date() - iDaysOld) Then 

' ********************************** 

' *** (optional) Comment the following line
			msgbox (oFile & " would be deleted, " & oFile.DateLastModified)

' *** and Uncomment the following line, which actually does the deletion.
'	 		oFile.Delete(True) 

' **********************************

 		End If 
 	End If   
Next 

Set oFSO = Nothing 
Set oFolder = Nothing 
Set oFileCollection = Nothing 
Set oFile = Nothing

Open in new window

0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 36481682
You could just grab a copy of this great utility, it excels at exactly this, for free.

http://www.horstmuc.de/win/delage.htm

http://www.horstmuc.de/wbat32.htm

~bp
0
 
LVL 7

Expert Comment

by:BobintheNoc
ID: 36482039
Since the folder structure suggests an originally WINNT 4 system, i'm guessing this ain't on a 2008 server?

Within Win7 and Win2008, there's a new piece on the command line, FORFILES, and it's used for stuff EXACTLY like you're wanting, all within CMD's internals.

Otherwise, should be lots to find on a download site for shareware.
0
 

Author Closing Comment

by:ex_ad
ID: 36813836
thx
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question