Server Fault is a question and answer site for system and network administrators. 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 know how to delete a single sub folder or multiple ones by mask:

rmdir /s /q C:\Somedir\DirToDelete


rmdir /s /q C:\Somedir\DirsToDelete*

But I want to delete all sub folders, say, with name DeleteMe, from all sub folders within C:\Somedir.


share|improve this question
up vote 3 down vote accepted

First you want to list all the directories you want to remove. Then use that to delete them all. Here's a quick and dirty example using what you put in there.

for /f "usebackq" %%a in (`"dir C:\Somedir /ad/b/s DirsToDelete"`) do rmdir "%%a"

Please test this out before you kick it off for your environment. I usually would replace the rmdir with echo to make sure it works.

share|improve this answer

Another way is (only PowerShell)

 Remove-Item -path e:\path\*\path\*\* -Filter *DeleteMe* -WhatIf

Remove all DeleteMe dirs recursive in last "asterisk" path.

-WhatIf = only show which dirs will remove, no do it - nice for check before destroy other data:)

UPDATE 1 After some labor... this update is better... line before can delete only empty folders. This can delete folder with content:

get-childitem -path E:\path\*\path\*\* -filter *DeleteMe* | remove-item -whatif -force -recurse
share|improve this answer
I'm pretty sure the OP wanted cmd/batch compatible commands... You should at least mention that this is PowerShell. – Chris S Jan 6 '14 at 19:39
True, notice added – Pavel Jan 6 '14 at 19:47
+1, and the OP should mention he wanted to use a batch script, as Powershell definitely counts as Windows command line nowadays. – BigHomie Jan 6 '14 at 20:00

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.