But the reality is, we're not really excluding the directory. Get-ChildItem is still processing every file in the Windows directory and passing it to Where. So you don't really gain anything over just doing a plain

It's not very fast, but works. This is probably one of those not many things that powershell script can do better.

Nice, a little Regex action. Of course, if you have a problem that you need Regex to solve, you really have two problems ;)

But I think this runs into the same problem I ran into with Powershell... you're still processing everything and filtering out the results. The real goal is to not process Windows at all and I just couldn't make that happen in Powershell. Might still be possible, I just couldn't figure it out!! ;)

outputs a list of top-level directories EXCEPT windows and progfiles, so the next two lines search within remaining folders only.

The problem is that test of file existence is pretty slow and it must be done for every subdirectory of qualified top-level directories. It would be quicker to do a search for the anything.exe file, trim out the directory it is contained in and delete it. The problem with batch scripting is that it has very poor string manipulation support.

Here is the script, which one is searching for "anything.exe" on C: drive and remove the folder in which is it:

iDrew, reconsider removing directory that contains anything.exe. Let's assume some user puts anything.exe on top of his profile folder (c:\users\username\anything.exe or c:\documents and settings\username\anything exe). The script will delete his profile completely! I suggest removing file itself instead or at least making a list of directories containing this file, for each machine, for you to verify if it's safe to remove the directories.

There are couple of strange folder name under C:\ drive (for example: feaa63031ade9dfdd2ab7c3dbe3360), which are remained service pack files. And I can find "spuninst.exe" file in all of these folders. I would like the remove these strange folders with this script.

outputs a list of top-level directories EXCEPT windows and progfiles, so the next two lines search within remaining folders only.

You're still processing those directories and piping into the FIND cmdlet, same problem I had. Yes the OUTPUT leaves those directories out but the input doesn't (the DIR command). Ideally we'd want to tell DIR to exclude the whole directory structure and just move on.

You're still processing those directories and piping into the FIND cmdlet, same problem I had. Yes the OUTPUT leaves those directories out but the input doesn't (the DIR command). Ideally we'd want to tell DIR to exclude the whole directory structure and just move on.

You're mistaken here or we're thinking about different things. DIR command indeed processes windows and progfile dirs, but only as top-level folders just to excude them, thus to make a list of top-level folders to search in. The script DOES NOT search for anything.exe within them at all, unlike your PS line. The adequate command line would be:

You're mistaken here or we're thinking about different things. DIR command indeed processes windows and progfile dirs, but only as top-level folders just to excude them, thus to make a list of top-level folders to search in. The script DOES NOT search for anything.exe within them at all, unlike your PS line. The adequate command line would be:

This one indeed would search both windows and progfiles folders and THEN exclude those results via piped find exclusion. That's the wrong approach, would hog HDD indeed.

Ahhh.. I see it now! I should have looked closer at the DIR command, didn't see the /ad! You could even add the /s to that and get subfolders that don't match those patterns. Nice!

Could do something similar with Get-ChildItem in Powershell but it's still processing EVERYthing and only outputting what you want. I wonder if DIR is doing the same thing or if it can truly only look at the directories?

No, no. I didn't mistype. This is the echo in CMD window when I run the script.

It looks like the dos misconceived the second FOR loop (and couldn't use the %%b variable).

Can you please attach your batch file, please? :)

I admit I didn't actually tested that script :P But here it goes, fixed and working. I dropped your bizarre for /d /r since I found it quite odd-behaving. Not sure how about you but I haven't found it documented to use /d and /r switches simultaneously.

Yes, there is %%~fa instead of %%a. It is necessary to retrieve full path to dir'ed top-level folders, and will also work if you search from within subfolder instead of c:\ itself. I also dropped /a switch for dir command, because it's probably pointless to search in hidden folders, like recycler. You can add it if you like, tho'.