Batch command to delete all subfolders with a specific name
Solution 1
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
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
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
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.
Failstyle
Updated on July 09, 2022Comments
-
Failstyle almost 2 years
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.
-
Alex almost 10 yearsyou would want to put this file into
D:\
if you want this to run onD:\
or add acd /d D:\
before thefor /d /r
loop -
Groo over 7 yearsTo use this in a batch file, you need to escape the
%
char, i.e. replace each%d
with a%%d
. -
dermoritz over 6 yearsnot working for me tried with (node_modules) but its not working - search from explorer shows hundrets of "node_modules" folders
-
Shital Shah over 5 yearsAbove 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"
. -
Łukasz Rząsa almost 5 yearsOn 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. -
Tjorriemorrie about 4 yearsI get
zsh: unknown file attribute: _
-
JDC about 4 years@Tjorriemorrie this code is for "windows dos" command line (aka cmd) and I think you're using Z shell (zsh)
-
Satadru Biswas almost 4 yearsTo run this directly in CMD prompt, replace
%%d
with%d
. -
maheshwaghmare about 3 yearsRight. 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 codeFOR /d /r . %%d IN ("node_modules") DO @IF EXIST "%%d" rd /s /q "%%d"
and it works. -
Michael Argentini over 2 yearsThis 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 over 2 yearsYou 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
-
luisdev over 2 yearsCan you specify a full path ("C:\Test\xxx\yyy") instead of just the "backdrops" folder name?