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)
(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:The third parameter,
[Action]
is optional. Without it the files older than<NoDaysSinceModified>
will be listed. With it set asD
it will delete files older than<NoDaysSinceModified>
Example
will list all files in
c:\Log Files
older than 8 days oldwill delete all files in
c:\Log Files
older than 8 days oldnote: this is a modified version of Haidong Ji's script on SQLServerCentral.com
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)
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.
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.
Another alternative I used to use before powershell:
http://lifehacker.com/133190/geek-to-live--hard-drive-janitor
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.