Solved

Script to clear log files which are 15 days old.

Posted on 2011-09-04
4
273 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
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 53

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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