7

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?

7

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. – Steve Chambers May 23 '18 at 12:38
  • %%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 at 16:18
6

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 '14 at 19:39
  • True, notice added – Pavel Jan 6 '14 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 '14 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 '17 at 15:26
0
  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.