Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory with thousands of html files scattered across several subdirectories. I need to delete all files matching any one of many criteria. All criteria are similar by kind. The file names are like:

So, all files that are ending in certain hostnames must be deleted. There are about 20 hostnames for now. (But that number will increase to about 100.) I store a list in txt file and prefer to do so.

The thing is, some "good" files contain "bad" hostnames but not at end of file name.

__www.onesite.com_.html (bad)
__www.onesite.com_prices.html (good)

Can I create a bach file to safely scan all subdirectories and remove unnecessary files using the list? OS is Windows 7. I want to set up Task Scheduler to run such butch each night. But I'm not familiar with commands. Please help.

P.S. In fact, some "bad" files have linked folders _files. I expect they will be deleted automatically like it happens when I manually delete main html file.

share|improve this question
We are not here to do your work for you. Try so solve it yourself and when you get stuck on a specific problem, ask about that. I can however say that batch isn't the correct tool for the job. There are several better scripting languages for windows you can use. For example powershell, perl, python or even javascript. – Nifle Aug 25 '13 at 11:18
@Nifle I apologize for disappearing. "Try so solve it yourself and when you get stuck on a specific problem, ask about that." Yes, I got it. I will try PowerShell. Thanks. – Mirosav Kostyakov Sep 7 '13 at 10:15

Just a quickie to get you started:

@echo off

for /F %%p in (list.txt) do (
    echo Current pattern: %%p
    for /R "C:\" %%f in (*%%p*) do (
        echo File: %%f

exit /B 0
share|improve this answer
This dosn't work for good files with bad host names. – Endoro Aug 26 '13 at 5:53
@Endoro: why not? – Vlastimil Ovčáčík Aug 27 '13 at 11:15
this will "delete" __www.onesite.com_prices.html – Endoro Aug 27 '13 at 11:20
Ah, I misunderstood the "bad" and "good" (poor) explanation in the question. – Vlastimil Ovčáčík Aug 27 '13 at 11:28
@Vlastimil, thank you for your time. I'm sorry for poor explanation. – Mirosav Kostyakov Sep 7 '13 at 10:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.