When working with large directory structures in Windows, you might encounter situations where you need to delete all instances of a specific subfolder name across multiple parent directories. The standard rmdir
command with wildcards doesn't handle this recursive scenario well.
The most efficient way to accomplish this is by combining the FOR /R
recursive directory traversal with the rmdir
command:
FOR /R "C:\Somedir" %d IN (DeleteMe) DO @IF EXIST "%d" rmdir /s /q "%d"
Let's analyze what this command does:
FOR /R "C:\Somedir"
- Recursively processes all subdirectories under C:\Somedir%d IN (DeleteMe)
- Looks for directories named exactly "DeleteMe"DO @IF EXIST "%d"
- Verifies the directory exists before attempting deletionrmdir /s /q "%d"
- Silently removes the directory and all its contents
For those who prefer PowerShell, here's an equivalent solution:
Get-ChildItem "C:\Somedir" -Directory -Recurse -Filter "DeleteMe" | Remove-Item -Recurse -Force
When running recursive deletion commands:
- Always test with
echo
first to see what would be deleted - Consider creating a backup before mass deletions
- Double-check your path and folder name to avoid accidental deletions
Test command (dry run):
FOR /R "C:\Somedir" %d IN (DeleteMe) DO @IF EXIST "%d" echo "%d"
For more complex scenarios where folder names might contain spaces or special characters:
FOR /R "C:\Somedir" %d IN ("Folder With Spaces") DO @IF EXIST "%d" rmdir /s /q "%d"
For repeated use, create a batch file (remember to double the % signs):
@echo off
FOR /R "C:\Somedir" %%d IN (DeleteMe) DO (
IF EXIST "%%d" (
echo Deleting "%%d"
rmdir /s /q "%%d"
)
)
pause
Working with large directory structures in Windows, I often need to clean up specific subfolders recursively. While rmdir /s /q
works great for deleting a single directory or pattern-matched directories, it doesn't handle recursive subfolder deletion based on name matching.
For modern Windows systems, PowerShell provides the most robust solution:
Get-ChildItem -Path "C:\Somedir" -Directory -Recurse -Force |
Where-Object { $_.Name -eq "DeleteMe" } |
Remove-Item -Recurse -Force
This command:
- Recursively searches all subdirectories under C:\Somedir
- Filters for directories named exactly "DeleteMe"
- Deletes them with all contents (-Recurse) without confirmation (-Force)
For environments where PowerShell isn't available, this batch script works:
@echo off
set "root=C:\Somedir"
set "target=DeleteMe"
for /d /r "%root%" %%a in (%target%) do (
if exist "%%a" (
echo Deleting "%%a"
rmdir /s /q "%%a"
)
)
For case-insensitive matching or partial name matches:
# PowerShell case-insensitive partial match
Get-ChildItem -Path "C:\Somedir" -Directory -Recurse -Force |
Where-Object { $_.Name -like "*delete*" } |
Remove-Item -Recurse -Force
For large directory structures:
- PowerShell is generally faster than CMD
- Add
-ErrorAction SilentlyContinue
to suppress permission errors - Consider adding
-Depth
parameter to limit recursion levels
Before running deletion commands:
# First test with -WhatIf parameter
Get-ChildItem -Path "C:\Somedir" -Directory -Recurse -Force |
Where-Object { $_.Name -eq "DeleteMe" } |
Remove-Item -Recurse -Force -WhatIf