60

I have a directory as such:

D:\Movies
D:\Movies\MovieTitle1\backdrops\
D:\Movies\MovieTitle2\backdrops\
D:\Movies\MovieTitle3\backdrops\
D:\Movies\MovieTitle4\backdrops\

How could I have a batch file delete all folders named "Backdrops"? I would prefer it to run recursive from just the D:\ drive if possible.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Failstyle
  • 681
  • 1
  • 7
  • 10

4 Answers4

101

Short answer:

FOR /d /r . %%d IN (backdrops) DO @IF EXIST "%%d" rd /s /q "%%d"

I got my answer from one of the countless answers to the same question on Stack Overflow:

Command line tool to delete folder with a specified name recursively in Windows?

This command is not tested, but I do trust this site enough to post this answer.

As suggested by Alex in a comment, this batch script should be foolproof:

D:
FOR /d /r . %%d IN (backdrops) DO @IF EXIST "%%d" rd /s /q "%%d"
PencilBow
  • 979
  • 11
  • 17
Ziga Petek
  • 3,953
  • 5
  • 33
  • 43
  • 3
    you would want to put this file into `D:\ ` if you want this to run on `D:\ ` or add a `cd /d D:\ ` before the `for /d /r` loop – Alex Aug 28 '14 at 17:15
  • 18
    To use this in a batch file, you need to escape the `%` char, i.e. replace each `%d` with a `%%d`. – vgru Dec 27 '16 at 22:06
  • not working for me tried with (node_modules) but its not working - search from explorer shows hundrets of "node_modules" folders – dermoritz Oct 26 '17 at 07:28
  • 2
    On Windows 7 i had to change part of the command to `"%d" rd /S /Q "%d"` (uppercase /s and /q). Otherwise the access to non-empty directories was denied. – Łukasz Rząsa Sep 11 '19 at 11:55
  • 5
    To run this directly in CMD prompt, replace `%%d` with `%d`. – Satadru Biswas Aug 29 '20 at 02:53
  • @Ziga Petek what if i want to delete the same directory mentioned in this question from all the partitions in my pc?not only D: drive..what or which parameter in your answer should be changed – Amrmsmb Jun 29 '23 at 10:06
  • @ŁukaszRząsa what if i want to delete the same directory mentioned in this question from all the partitions in my pc?not only D: drive..what or which parameter in your answer should be changed – Amrmsmb Jun 29 '23 at 10:06
22

Above answer didn't quite work for me. I had to use a combination of @itd solution and @Groo comment. Kudos to them.

Final solution for me was (using the backdrop folder example):

FOR /d /r . %%d IN ("backdrops") DO @IF EXIST "%%d" rd /s /q "%%d"
JDC
  • 3,078
  • 1
  • 17
  • 15
  • 3
    Above is good for command in batch file. If you are want this to type at command line then do `FOR /d /r . %d IN ("__pycache__") DO @IF EXIST "%d" rd /s /q "%d"`. – Shital Shah Feb 22 '19 at 23:42
  • I get `zsh: unknown file attribute: _` – Tjorriemorrie May 26 '20 at 07:14
  • @Tjorriemorrie this code is for "windows dos" command line (aka cmd) and I think you're using Z shell (zsh) – JDC May 26 '20 at 15:13
  • 2
    Right. It works for me too. I had a lot of 3k+ node_modules directoryies from 2/3 drives. So, To delete all of them I have created the `cmd.bat` file and added the code ```FOR /d /r . %%d IN ("node_modules") DO @IF EXIST "%%d" rd /s /q "%%d"``` and it works. – maheshwaghmare Apr 01 '21 at 10:16
  • Can you specify a full path ("C:\Test\xxx\yyy") instead of just the "backdrops" folder name? – luisdev Feb 04 '22 at 09:35
17

I will open a different answer, because it would be too cramped in the comments. It was asked what to do, if you want to execute from/to a different folder and I want to give an example for non-recursive deletion.

First of all, when you use the command in cmd, you have to use %d, but when you use it in a .bat, you have to use %%d.

You can use a wildcard to just process folders that for example start with "backdrops": "backdrops*".

Recursive deletion of folders starting in the folder the .bat is in:

FOR /d /r . %d IN ("backdrops") DO @IF EXIST "%d" rd /s /q "%d"

Non-recursive deletion of folders in the folder the .bat is in (used with wildcard, as you cannot have more than one folder with the same name anyway):

FOR /d %d IN ("backdrops*") DO @IF EXIST "%d" rd /s /q "%d"


Recursive deletion of folders starting in the folder of your choice:

FOR /d /r "PATH_TO_FOLDER" %d IN ("backdrops") DO @IF EXIST "%d" rd /s /q "%d"

Non-recursive deletion of folders in the folder of your choice (used with wildcard, as you cannot have more than one folder with the same name anyway):

FOR /d %d IN ("PATH_TO_FOLDER/backdrops*") DO @IF EXIST "%d" rd /s /q "%d"

user136036
  • 11,228
  • 6
  • 46
  • 46
6

I look at this question from the .Net developer's point of view. Sometimes it is needed to wipe all */bin/ and */obj/ subfolders recursively starting from the directory from which the batch script is executed. I tried abovementioned solutions and sighted a crutial point:

Unlike other variants of the FOR command you must include a wildcard (either * or ?) in the 'folder_set' to get consistent results returned.

Source: https://ss64.com/nt/for_d.html

When adding echo for each found result before deleting it we can ensure that there are no false positive matches. When I have done so, I found out that using (obj) folder_set without a wildcard triggers DO expression for each subfolder even if it doesn't match a mask. E.g. deleting the "/.git/objects/" dir which is bad. Adding a question mark (0 or 1 occurrence of any symbol except dot) at the end of the mask solves this issue:

@echo off

FOR /d /r %%F IN (obj?) DO (
    echo deleting folder: %%F
    @IF EXIST %%F RMDIR /S /Q "%%F"
)

FOR /d /r %%F IN (bin?) DO (
    echo deleting folder: %%F
    @IF EXIST %%F RMDIR /S /Q "%%F"
)

The same goes for any other masks. E.g. (packages?) and (node_modules?) to wipe cached libraries for making a backup archive more lightweight.

Dudar
  • 719
  • 6
  • 8
  • 2
    This works well, but you need to put double quotes around the %%F variable in the RMDIR line, so long paths don't get skipped. ```@IF EXIST %%F RMDIR /S /Q "%%F"``` And very long paths may still yield errors. – Michael Argentini Dec 29 '21 at 17:49
  • 1
    You can also create an empty directory at the top of your batch file: `mkdir \empty` and then insert this line above each RMDIR line to purge the contents prior to removal. This will handle long path items that can't be removed by RMDIR: `@IF EXIST %%F robocopy "\empty" "%%F" /MIR`. Finish the batch file by removing the empty directory: `rmdir /empty` – Michael Argentini Dec 29 '21 at 18:15