Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

A Small Utility to Delete Files recursively by Date


:P
On this page:

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.

Posted in Windows  CSharp  

The Voices of Reason


 

Will
December 01, 2012

# re: A Small Utility to Delete Files recursively by Date

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. ^_^

David Sheeks
December 01, 2012

# re: A Small Utility to Delete Files recursively by Date

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

Rick Strahl
December 02, 2012

# re: A Small Utility to Delete Files recursively by Date

@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.

Rick Strahl
December 02, 2012

# re: A Small Utility to Delete Files recursively by Date

@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!

Brad
December 04, 2012

# re: A Small Utility to Delete Files recursively by Date

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

dacanetdev
December 05, 2012

# re: A Small Utility to Delete Files recursively by Date

Nice utility and very clean code

Tim Clark
January 04, 2013

# re: A Small Utility to Delete Files recursively by Date

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.

Kenneth Spark
September 22, 2014

# re: A Small Utility to Delete Files recursively by Date

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.

Aaron
September 23, 2014

# re: A Small Utility to Delete Files recursively by Date

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 :)

Rick Strahl
September 23, 2014

# re: A Small Utility to Delete Files recursively by Date

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.

Marcio
February 20, 2015

# re: A Small Utility to Delete Files recursively by Date

Hi Rick,

I tried using your utility (the binary) on Windows 8 64 bit and Windows gave me an error on compatibility with 64 bit versions of Windows.

Thanks anyway

Rick Strahl
February 20, 2015

# re: A Small Utility to Delete Files recursively by Date

@Marcio - Any more specifics on the error? I run it on 64 bit here and on my server so pretty sure that works, unless there's a problem with Anti-Virus or other system protection software interfering.

If you have more info can you file an issue in the GitHub repo, please? Thanks.

Herbert
August 17, 2015

# re: A Small Utility to Delete Files recursively by Date

Hi,

I'm looking for a solution where I can use the '*' char in the middle of the pathname, like below.

DeleteFiles f:\ABC\*\XYZ\*.* -r -f -d10

The '*' in the middle of the path can be 1 or more sub folders

Does this tool work with this?

Kind regards, Herbert

Rick Strahl
August 17, 2015

# re: A Small Utility to Delete Files recursively by Date

No that's not supported - wildcards can't be applied to folders only to the file filter.

Simon Weel
May 10, 2017

# re: A Small Utility to Delete Files recursively by Date

Can I specify multiple file specifications, like **.bak; .bak2 etc.


John
May 14, 2018

# re: A Small Utility to Delete Files recursively by Date

Love the app, great to see people coding C rather than scripting. Is there any way to have this tool loop thru and delete any empty folders without deleting any files?

Thought i could do like deletefiles.exe d:\folder -f but it doesn't do anything.

Thanks.

JR


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