Solved

Script to clear log files which are 15 days old.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 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