Last year I posted about a small DeleteFiles utility I created that once and for all solved my problem of recursively deleting files and folders down a Windows folder hierarchy from the command line. On my server I constantly need to delete backup database and log files and other temporary file junk that piles up as part of 20 odd Web sites running their thing. On my local machine today I frequently need to clean out development package management folders for NPM or NuGet with specific folder specs. On my server I have scheduled tasks that fire batch files running DeleteFiles to clean out temp folders and backup folders. So for me deleting files based on a file spec is a common occurrence and DeleteFiles has been a really handy tool to make this easy with a simple single file executable.
On the previous post there were a number of comments on how to accomplish this sort of thing just with the Windows command prompt using various helper tools or some goobledygook Powershell, but for me personally I prefer a simple solution that has a clearly defined purpose and syntax that is easily remembered without going on a pony hunt every time I need this facility.
So for this reason DeleteFiles has the following simple and focused feature set:
- Delete files with simple file specs like c:\temp\*.* or *.tmp
- Recursively delete files down the folder hierarchy (-r switch)
- Optionally delete empty folders (-f switch)
- Filter files to delete based on days or seconds before current time (-d -s)
- Files can be optionally dumped into the Recycle Bin for recoverable deletes (-y)
- Preview mode lets you test run to see what would get deleted (-l)
- Works with deeply nested folder hierarchies (like NPM folders)
- Works around Windows MAX_PATH limitations
- Portable, single-file EXE file
You can grab the executable either from Chocolatey or from GitHub along with the source code:
Since the original version came out there have been a few additional features that have been added.
Long File Paths – greater than MAX_PATH
Windows does allow for a workaround by using special path syntax (
\\?\D:\very long path.) but you can’t use that easily in Explorer or the command line and the .NET BCL components also don’t actually support that natively .
I ended up adding support for greater MAX_PATH lengths by integrating the excellent ZetaLongPaths library and that’s a big improvement. In the process also a couple of other issues dealing with file locking have and read-only files have been addressed.
DeleteFiles can now be installed via Chocolatey
It took a while to get the package approved (growing pains at Chocolatey I suppose), but you can easily get DeleteFiles installed via the Chocolatey NuGet package. If you don’t know what Chocolatey is, do yourself a favor and head over to their site and install it immediately. Chocolatey is a NuGet like package manager for Windows that allows you easily discover and install many Windows Utilities and Applications quickly and without running installers explicitly. You can combine and automate installs easily in batch files and keep all of your utilities up to do data. These days for new machine installs I install 90% of all apps I use via Chocolatey. Yummy!
There’s now also a quiet mode that allows you to control the detail of messages that get spewed out. By default all files to be deleted are shown, but you can quiet that down to just a summary or no output at all other than the running banner.
Better safe than sorry: Preview Mode and Recycle Bin
You can now do a what-if run of your delete operations that shows you exactly what’s going to be deleted without actually deleting it. By default DeleteFiles hard deletes files but you can also use the –y option to delete files/folders to the recycle bin just in case you screw up by accident.
Try it out
This utility is very small and self contained in a single exe and has a very narrow use case, but to me it’s become something I use all the time. I have it in my global path (via Chocolatey’s install) so it’s easy to get to it quickly. I may not need it often but when I do it’s quick and easy to find and use with a simple command line interface that I can remember. Maybe you find it useful as well.
Other Posts you might also like