Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
2  
A new method based in a .BAT file that use internal CMD.EXE commands only has been posted here: stackoverflow.com/questions/9746778/… –  user114382 Mar 17 '12 at 3:57

7 Answers 7

up vote 33 down vote accepted

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)

share|improve this answer
    
To confirm, does this permanently delete the files or recycle them? –  TimS Oct 18 '13 at 11:11
    
The link is broken. Could you update it? –  Gennady Vanin Геннадий Ванин Oct 25 '13 at 1:40
    
@Ген Best way is to look it up in the wayback machine at the Internet Archive –  Jeff Atwood Oct 26 '13 at 8:53
    
Ops too hard for memorize!!i love powershell but i think better way is using robocopy –  Jeson Park Sep 16 at 8:56

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

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.

share|improve this answer

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.

share|improve this answer

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

Another alternative I used to use before powershell:

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

share|improve this answer

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.

share|improve this answer

protected by Chris S May 18 '12 at 14:46

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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