31

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.

44

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)

  • 1
    To confirm, does this permanently delete the files or recycle them? – TimS Oct 18 '13 at 11:11
  • Ops too hard for memorize!!i love powershell but i think better way is using robocopy – AminM Sep 16 '14 at 8:56
7

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
  • I use something like this to delete old web server logs. Has worked out pretty well. – jeffspost Aug 4 '09 at 13:32
4

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);
  • Linq is really awesome, I wish the would bring in to PowerShell. – Taylor Gibb Dec 22 '12 at 17:11
4

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.

3

Something similar can be achieved with cygwin's (or other alternative) "find" command. But this would require you to install cygwin or have the portable version at hand.

2

Another alternative I used to use before powershell:

http://lifehacker.com/133190/geek-to-live--hard-drive-janitor

2

I use autoIT to accomplish this on my systems. I like that you can compile .au3 files to exe easily. Not as easy to introduce a security flaw as it is with a bat file anyone can edit.

Not the answer you're looking for? Browse other questions tagged or ask your own question.