Deleting Files with an Extension Recursively Down Directories

Windows Server and IIS

Most of us have been there. We’re using some sort of a program which does something automatically. Make it’s making log files. Maybe it’s doing automated updates on files. The next thing we know, all of our directories are littered with files ending in .LOG or .BAK or something else.

Sure, if you have one or two of these files, it’s no problem deleting them. But what if you have hundreds of folders of these files? Thousands of them?

This happened to me. I use TextCrawler to make search-and-replace changes to entire websites at a time. It’s an incredibly useful tool to change, for example, a copyright year from 2019 to 2020. But if you don’t check your settings carefully, it can create a .BAK file for EVERY SINGLE FILE ON YOUR WEBSITE.

Talk about a pain in the butt to take care of.

That’s where this useful command line command comes into play.

What you do is open a command prompt. navigate to the top level directory you want to work with.

Type in:

DEL /S /Q *.BAK

The DEL command is to delete things.

The /S parameter is to do move through subdirectories. Otherwise it’d just do it at the level of directory it is at.

The /Q command is to do it quietly. Otherwise you’ll get a prompt about ‘are you sure?’ with every single file being deleted. This could drive you Absolutely. Insane.

The *.BAK command is to do anything ending in .BAK. You would change that to whatever extension you are trying to deal with.

I will note that you should ALWAYS back up your files before attempting something like this. Things go wrong. It’s the nature of the universe. Be prepared so you can recover.

Be the first to comment

Leave a Reply

Your email address will not be published.


*