Solved

Script to Delete old log files

Posted on 2009-04-06
5
1,206 Views
Last Modified: 2012-08-13
Basically I'm looking for a script to delete all log files in the C:\Windows\System32\LogFiles\ directory that are older than 1 month. I found the script below, but wasn't sure how to taylor it to get a dynamic date. Also I wasn't sure if I could just change "AND Drive = 'D:'" to ='C:\Windows\System32\LogFiles\" Thanks!
strDate = "20031102000000.000000+000"
 

strComputer = "."

Set objWMIService = GetObject _

    ("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _

    ("Select * From CIM_DataFile Where CreationDate < '" & strDate & "'" & _

        " AND Drive = 'D:'")

For Each objFile in colFiles

    Wscript.Echo objFile.Name

Next

Open in new window

0
Comment
Question by:jonmclean2
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 24078324
It might be easier to customize the vbscript from this article.

You could also use the forfiles command in a batch script, e.g.


forfiles /p "%systemroot%\System32\LogFiles" /m *.log /c "cmd /c ECHO del @file" /d -30

Open in new window

0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 24078390
Whoops, change @file to @path.  

As written, the command just echoes the paths of the files.  Once you've tested it successfully, remove the ECHO to execute the deletions.
0
 
LVL 1

Author Comment

by:jonmclean2
ID: 24099868
Forfiles /p "C:\WINDOWS\System32\LogFiles" /m *
.log /c "cmd /c ECHO del @path" /d -30

'Forfiles' is not recognized as an internal or external command,
operable program or batch file.
0
 
LVL 1

Author Comment

by:jonmclean2
ID: 24099926
Apologies i was attempting to test it on my local machine. When I run it on the server I get this.

C:\Documents and Settings\baerme.SAIC-US-EAST>forfiles /p "%systemroot%\System32
\LogFiles\W3SVC400752808\" /m *.log /c "cmd /c ECHO del @path" /d -30
ERROR: Invalid argument/option - 'del'.
Type "FORFILES /?" for usage.
0
 
LVL 1

Author Comment

by:jonmclean2
ID: 24100277
forfiles /p "%systemroot%\System32\LogFiles\W3SVC400752808\" /m *.log /c "cmd /c if @isdir==FALSE echo del @file & del @file" /d -9 Worked
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now