Script to Delete old log files

Posted on 2009-04-06
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

Open in new window

Question by:jonmclean2
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
  • 3
  • 2
LVL 38

Expert Comment

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

LVL 38

Accepted Solution

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.

Author Comment

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.

Author Comment

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.

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
In this video we show how to create a Contact 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 Recipients >> Contact ta…
how to add IIS SMTP to handle application/Scanner relays into office 365.
Suggested Courses

631 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