Batch command to delete all subfolders with a specific name

Batch FileWindowXbmc

Batch File Problem Overview


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.

Batch File Solutions


Solution 1 - Batch File

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"

Solution 2 - Batch File

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"

Solution 3 - Batch File

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"

Solution 4 - Batch File

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFailstyleView Question on Stackoverflow
Solution 1 - Batch FileitdView Answer on Stackoverflow
Solution 2 - Batch FileJDCView Answer on Stackoverflow
Solution 3 - Batch Fileuser136036View Answer on Stackoverflow
Solution 4 - Batch FileDudarView Answer on Stackoverflow