Batch command to delete all subfolders with a specific name
Asked Answered
S

5

63

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.

Salvation answered 28/8, 2014 at 17:3 Comment(0)
F
106

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"
Finally answered 28/8, 2014 at 17:9 Comment(7)
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 loopIntermarriage
To use this in a batch file, you need to escape the % char, i.e. replace each %d with a %%d.Indicant
not working for me tried with (node_modules) but its not working - search from explorer shows hundrets of "node_modules" foldersDisorganization
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.Oftentimes
To run this directly in CMD prompt, replace %%d with %d.Breccia
@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 changedOptional
@Ł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 changedOptional
O
23

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"
Omega answered 16/11, 2018 at 16:3 Comment(5)
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".Asiaasian
I get zsh: unknown file attribute: _Screwed
@Screwed this code is for "windows dos" command line (aka cmd) and I think you're using Z shell (zsh)Omega
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.Itinerary
Can you specify a full path ("C:\Test\xxx\yyy") instead of just the "backdrops" folder name?Krug
P
19

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"

Pressmark answered 28/3, 2019 at 10:55 Comment(0)
H
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.

Horsy answered 16/8, 2021 at 14:38 Comment(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.Arabinose
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 /emptyArabinose
B
0

Building on the answers already provided, the below batch file will accept a folder path as a parameter and will recursively process it. The batch file can be saved in any folder/drive of your choice and does not have to be located on the drive where the folder to be processed is located.

The batch file also demonstrates the ability to specify multiple folders to be deleted which is not obvious from the answers already provided. Just replace folder1, folder2, folder3 with the actual folder names which you want to delete.

Note that the names of the folders to be deleted must end with a question mark as per Dudar's findings in his answer.

@Echo Off
If "%~1"=="" (
 Echo Folder path parameter is required.
) Else (
 If Exist "%~1\" (
  For /D /R "%~1" %%x In ("Folder1?", "Folder2?", "Folder3?") Do (
   If Exist "%%x" (
    Echo Deleting folder "%%x"
    RD /S /Q "%%x"
   )
  )
 ) Else (
  Echo Folder path does not exist.
 )
)
Pause
Blanketyblank answered 17/3 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.