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.
The Voices of Reason
# re: A Small Utility to Delete Files recursively by Date
# re: A Small Utility to Delete Files recursively by Date
# re: A Small Utility to Delete Files recursively by Date
# re: A Small Utility to Delete Files recursively by Date
http://codejournal.blogspot.com/2011/05/file-or-folder-delete-utility.html
# re: A Small Utility to Delete Files recursively by Date
# re: A Small Utility to Delete Files recursively by Date
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
# re: A Small Utility to Delete Files recursively by Date
# re: A Small Utility to Delete Files recursively by Date
# re: A Small Utility to Delete Files recursively by Date
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
# re: A Small Utility to Delete Files recursively by Date
If you have more info can you file an issue in the GitHub repo, please? Thanks.
# re: A Small Utility to Delete Files recursively by Date
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
# re: A Small Utility to Delete Files recursively by Date
# re: A Small Utility to Delete Files recursively by Date
Can I specify multiple file specifications, like **.bak; .bak2 etc.
# 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
# re: A Small Utility to Delete Files recursively by Date
# Delete all files order than 7 days under C:\Windows\Temp directory recursively:
# FORCE delete all files order than 7 days under C:\Windows\Temp directory recursively:
Just for your reference. ^_^