Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I know how to delete a single sub folder or multiple ones by mask:

rmdir /s /q C:\Somedir\DirToDelete

or

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.

Ideas?

share|improve this question
add comment

2 Answers 2

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
add comment

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
2  
I'm pretty sure the OP wanted cmd/batch compatible commands... You should at least mention that this is PowerShell. –  Chris S Jan 6 at 19:39
    
True, notice added –  Pavel Jan 6 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 at 20:00
add comment

Your Answer

 
discard

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.