Delete Files Older than (x) Days?

What's a good Windows command line option for deleting all files in a given folder older than (n) days?

Also note there may be many thousands of these files, so forfiles with a shell to cmd is not a great idea here.. unless you like spawning thousands of command shells. I consider that a pretty nasty hack, so let's see if we can do better!

Ideally, something built into (or easily installable into) Windows Server 2008.


I looked around a bit more and found a powershell way:

Delete all files more than 8 days old from the specified folder (with preview)

dir |? {$_.CreationTime -lt (get-date).AddDays(-8)} | del -whatif

(remove the -whatif to make it happen)


Love Jeff's PowerShell command, but for an alternative vbs solution for Windows machines without PowerShell you could try the following.

Save as <filename>.vbs and execute:

<filename>.vbs <target_dir> <NoDaysSinceModified> [Action]

The third parameter, [Action] is optional. Without it the files older than <NoDaysSinceModified> will be listed. With it set as D it will delete files older than <NoDaysSinceModified>

Example

PurgeOldFiles.vbs "c:\Log Files" 8

will list all files in c:\Log Files older than 8 days old

PurgeOldFiles.vbs "c:\Log Files" 8 D

will delete all files in c:\Log Files older than 8 days old

note: this is a modified version of Haidong Ji's script on SQLServerCentral.com

Option Explicit
on error resume next
    Dim oFSO
    Dim sDirectoryPath
    Dim oFolder
    Dim oFileCollection
    Dim oFile
    Dim iDaysOld
    Dim fAction

    sDirectoryPath = WScript.Arguments.Item(0)
    iDaysOld = WScript.Arguments.Item(1)
    fAction = WScript.Arguments.Item(2)
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    set oFolder = oFSO.GetFolder(sDirectoryPath)
    set oFileCollection = oFolder.Files

If UCase(fAction) = "D" Then
'Walk through each file in this folder collection. 
'If it is older than iDaysOld, then delete it.
    For each oFile in oFileCollection
        If oFile.DateLastModified < (Date() - iDaysOld) Then
            oFile.Delete(True)
        End If
    Next
else
'Displays Each file in the dir older than iDaysOld
    For each oFile in oFileCollection
        If oFile.DateLastModified < (Date() - iDaysOld) Then
            Wscript.Echo oFile.Name & " " & oFile.DateLastModified
        End If
    Next
End If


'Clean up
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oFileCollection = Nothing
    Set oFile = Nothing
    Set fAction = Nothing

Not really command line, but I like using LINQPad as a C# scripting host:
(which just gave me an idea for a command line C# scripting thingie à la vbs files)

var files = from f in Directory.GetFiles(@"D:\temp", "*.*", SearchOption.AllDirectories)
            where File.GetLastWriteTime(f) < DateTime.Today.AddDays(-8)
            select f;

foreach(var f in files)
    File.Delete(f);

Have a look at this http://sourceforge.net/projects/delold as this is what I use.

simple but works. delold -d 14 Deletes files older that 14 days in the current folder.