Contact   •   Products   •   Search

Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs

A Small Utility to Delete Files recursively by Date


It's funny, but for me the following seems to be a recurring theme: Every few months or years I end up with a host of files on my server that need pruning selectively and often under program control. Today I realized that my SQL Server logs on my server were really piling up and nearly ran my backup drive out of drive space. So occasionally I need to check on that server drive and clean out files.

Now with a bit of work this can be done with PowerShell or even a complicated DOS batch file, but heck, to me it's always easier to just create a small Console application that handles this sort of thing with a full command line parser and a few extra options, plus in the end I end up with code that I can actually modify and add features to as is invariably the case. No more searching for a script each time :-)

So for my typical copy needs the requirements are:

  • Need to recursively delete files
  • Need to be able to specify a filespec (ie. *.bak)
  • Be able to specify a cut off date before which to delete files
  • And it'd be nice to have an option to send files to the Recycle bin just in case for operator error :-)
    (and yes that came in handy as I blew away my entire database backup folder by accident - oops!)

The end result is a small Console file copy utility that I popped up on Github:

https://github.com/RickStrahl/DeleteFiles

The source code is up there along with the binary file you can just run.

Using this utility you can delete files like this:

Remove all *.bak files recursively from f:\database\backups that are older than 10 days

DeleteFiles f:\backups\database\*.bak -r -f -d10

Creating DeleteFiles

It's pretty easy to create a simple utility like DeleteFiles of course, so I'm not going to spend any talking about how it works. You can check it out in the repository or download and compile it. The nice thing about using a full programming language like C over something like PowerShell or batch file is that you can make short work of the recursive tree walking that's required to make this work.

There's very little code, but there's also a very small, self-contained command line parser in there that might be useful that can be plugged into any project - I've been using it quite a bit for just about any Console application I've been building.

If you're like me and don't have the patience or the persistence (that funky syntax requires some 'sticking with it' that I simply can't get over) to get into Powershell coding, having an executable file that I can just copy around or keep in my Utility directory is the only way I'll ever get to reuse this functionality without going on a wild search each time :-)

Anyway, hope some of you might find this useful.

Make Donation
Posted in Windows  CSharp  


Feedback for this Post

 
# re: A Small Utility to Delete Files recursively by Date
by Will December 01, 2012 @ 10:46am
Windows has a build-in command called "forfiles.exe" under C:\Windows\System32 folder. Using this command can also easily delete files recursively by date. For example:

# Delete all files order than 7 days under C:\Windows\Temp directory recursively:
forfiles /P c:\Windows\Temp /S /D -7 /C "cmd /c del @path"


# FORCE delete all files order than 7 days under C:\Windows\Temp directory recursively:
forfiles /P c:\Windows\Temp /S /D -7 /C "cmd /c del /f @path"


Just for your reference. ^_^
# re: A Small Utility to Delete Files recursively by Date
by David Sheeks December 01, 2012 @ 4:03pm
Rick - Nice utility. I've enjoyed many of your posts. If you haven't heard of it, JPSoft (no affiliation, just a user since the early days of 4DOS) makes a very nice cmd.exe replacement shell for Windows that adds many useful options to commands like del, etc. They have a free "light" version. I use tcc.exe every day and it's the first thing I install when setting up a new Windows environment. Worth a look: http://jpsoft.com
# re: A Small Utility to Delete Files recursively by Date
by Rick Strahl December 02, 2012 @ 1:11pm
@David - yeah I use a custom Explorer shell myself (xPlorer2) and it can do this sort of find/delete interactively real easily too (although it doesn't extend the command line commands). But I tend to use this sort of thing on my servers and I don't want to install anything extra there.
# re: A Small Utility to Delete Files recursively by Date
by Rick Strahl December 02, 2012 @ 2:30pm
@Will - crap! I ran into ForFiles when I was looking for a solution last actually, but decided not to go that route because I thought (and the article I read said) it's part of the Windows SDK which is always a pain to install and keep updated. Looks like it's moved into the main Windows distribution which is sweet. Will definitely look at that for future stuff. Thanks for the reminder!
# re: A Small Utility to Delete Files recursively by Date
by Brad December 04, 2012 @ 1:21pm
Forfiles looks helpful, but still not what I needed. I have a similar deleter that doesn't have much of a command line parser, but it uses the pipeline filter pattern to easily add in more filters (we needed files beginning with numeric, files whose names were guids, etc.).
http://codejournal.blogspot.com/2011/05/file-or-folder-delete-utility.html
# re: A Small Utility to Delete Files recursively by Date
by dacanetdev December 05, 2012 @ 9:58am
Nice utility and very clean code
# re: A Small Utility to Delete Files recursively by Date
by Tim Clark January 04, 2013 @ 6:34am
I used to raise my eyebrows at powershell too, but I have to say, I've started to really dig it.

It's actually not that ugly at all. Example:

Get-ChildItem C:\temp\ -Include *.cs -Recurse | where {$_.CreationTime.AddDays(10) -lt (Get-Date) } | Remove-Item -WhatIf
 
#Or if you find the {$_.} synxtax weird, you can do this which is awfully close to C#
 
foreach ($file in Get-ChildItem C:\temp\ -Include *.cs -Recurse ) {
    if ($file.CreationTime.AddDays(10) -lt (Get-Date) ){
        Remove-Item $file -WhatIf
    }    
}


Of course one of the huge benefits is that you can use the -WhatIf parameter and have powershell tell you what it *would* delete without actually doing it. That's pretty slick.
# re: A Small Utility to Delete Files recursively by Date
by Kenneth Spark September 22, 2014 @ 1:55pm
This utility worked great. I needed something to clean up lots of old text files on a regular basis. I tried the forfiles .. but that performed much slower. So slow in fact that the windows scheduler killed the job.
# re: A Small Utility to Delete Files recursively by Date
by Aaron September 23, 2014 @ 9:50am
I haven't downloaded the utility yet, but will it accept various wildcards? I have some people doing massive amounts of testing against a server I've built, creating thousands of files and very strangely named folders each day. Currently I have a dummy script using question marks as wildcards to delete the oddly named folders. They now want the ability to run a script at will that will delete all (specific) files that are older than 2 hours (though I told them I can probably only do days). Hoping your tool will work for me, I'm really not a scripting guy :)
# re: A Small Utility to Delete Files recursively by Date
by Rick Strahl September 23, 2014 @ 1:09pm
Aaron - yes it does normal command prompt wildcards for file selection. And of course you can run it multiple times for varying combinations of deletions if necessary.
 


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2014