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.


3 Answers 3


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.

  • 2
    rmdir will only remove empty directories so in some cases rmdir /s /q "%%a" might be more appropriate. May 23, 2018 at 12:38
  • 2
    %%a is for Batch file, use %a for a command-line. For me, the working solution is (including support for space in directory name): for /f "usebackq delims=^=^" %a in (`"dir DirsToDelete /ad/b/s"`) do rmdir /s /q "%a"
    – Matt Roy
    Feb 7, 2019 at 16:18

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\*\* -recurse -filter *DeleteMe* | remove-item -whatif -force -recurse
  • 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, 2014 at 19:39
  • True, notice added
    – Pavel
    Jan 6, 2014 at 19:47
  • 1
    +1, and the OP should mention he wanted to use a batch script, as Powershell definitely counts as Windows command line nowadays.
    – MDMoore313
    Jan 6, 2014 at 20:00
  • I had to add '-recurse' in order to get this to work like so: get-childitem <strong> -recurse </strong>` -path C:\dev* -filter target | remove-item -whatif -force -recurse`
    – Darrin
    Feb 16, 2017 at 15:26
  • +1 I like the way it doesn't have double quotes so I can put the folder name in double quotes for folders with spaces in the name, also the fact that I could remove (for example) cover and covers folders with cover* is nice. Apr 15, 2022 at 20:11
  1. Create a bat file, update and paste below code

FOR /d /r . %%d IN ("Your folder name to be deleted") DO @IF EXIST "%%d" rd /s /q "%%d"

  1. Copy this bat file to your parent folder (could be C: or D: too)
  2. Run it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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