views:

5243

answers:

7

Hello,

I want to run a scheduled windows task that deletes that deletes all files from a directory that are older than 2 weeks.

The reason is that these are IIS and Tomcat logs that fill up my server, but I want to keep the most recent logs in case I need to investigate a problem.

Does any one know an easy way to do this?

Cheers

Nige

+1  A: 

Why dont you write a batch file or a powershell script and schedule it?

Script for deleting files older than a specified date.

Gulzar
+4  A: 

Schedule a batch file to handle this.

This line will delete all files (*.*) in c:\mydirectory that are older than 14 days:

FORFILES -pc:\mydirectory -s -m*.* -d-14 -c"DEL @FILE"

Put that in a text file, rename it to something like "deletefiles.bat" and schedule it.

I haven't tested this, but should be easy enough to try.


EDIT: If you use this, make sure you understand what is happening - the -s flag tells it to recurse the subdirectories, and that may not be what you want to happen. Also, you may need to specify some flags for the DEL command too. :)


EDIT: Realized that you need to download stuff from Microsoft in order for FORFILES to work. I like the accepted solution too, since you don't have to have anything special. The only problem is that it only happens every two weeks instead of running a process daily to remove all things older than 14 days. For what that's worth. :P

Jason Bunting
When running this I get the errorcan't execute DEL <filename> <error 2>.Any ideas why or what might cause this?
Jona
Off of the top of my head, because I don't have time to look at the moment, I imagine it has something to do with running the script as an administrator. You are probably going to have to throw a 'runas' command in there somewhere. Look up the syntax, but it would probably go in that last quoted part, like "runas <runas options> DEL @FILE". Let me know what you find out, I have not run this script since I hoped onto Windows 7 last summer, which is probably why I have not run into this.
Jason Bunting
Duh - you can probably just have the batch file run as an administrator, and skip having it spit it out in each "DEL @FILE" line... I really need more sleep!
Jason Bunting
A: 

It's fairly trivial to do if you have a perl (or similar) installed on the server:

#!perl
foreach my $file (</path/to/logs/*.log>) {
  next unless -M $file > 14;
  print "Deleting $file...\n";
  # unlink $file or die "Failed to remove $file: $!";
}

The line that actually does the delete is commented out, since there might be kids in the house :)

zigdon
+4  A: 

The simplest way would be a .bat run file run weekly or monthly.

cd \mylog\dir
mkdir archive
del /Q .\archive\*.log
move *.log .\archive

If you want something more complex look into downloading the cygwin tools to use un*x like commands, or possibly look into Powershell.

Mark Nold
+3  A: 

With VBScript, adapted from ScriptingAnswers

Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")

startFolder = "E:\temp"           ' folder to start deleting (subfolders will also be cleaned)

OlderThanDate = DateAdd("d", -07, Date)  ' 07 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
    Dim folder, file, fileCollection, folderCollection, subFolder

    Set folder = fso.GetFolder(folderName)

    Set fileCollection = folder.Files

    For Each file In fileCollection
        If file.DateLastModified < BeforeDate Then
            ' fso.DeleteFile(file.Path)    # Modify this to delete after testing
            WScript.StdOut.WriteLine (file.Path)
        End If
    Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
        DeleteOldFiles subFolder.Path, BeforeDate
    Next

End Function

You can run this script with CScript

@Jason: nice utility, FORFILES from the Resource Kit

PabloG
A: 

From example above by zigdon

This GLOB does not work with windows ( the program just hangs )

foreach my $file ()

This one will grab all files

foreach my $file () and works ... still I need just the *.pdf files.

Anyone know how to Modify Zigdons script above so it will glob properly in windows environment?

Thanks

Greg

+1  A: 

FORFILES /p d:\new /d -30 /m * /c "cmd /c del @file" is the exact syntex

subha