Recursively find and delete a folder using a batch file

I am trying to write a simple batch file that will find and delete a folder recursively. But the following script does not refer to a subfolder. Wondering how to do this?

@echo off
cd /d "C:\"
for /d %%i in (temp) do rd /s "%%i"




source to share

2 answers

for /d /r "c:\" %%a in (temp\) do if exist "%%a" echo rmdir /s /q "%%a"


For each folder ( /d

) recursively ( /r

) in c:\

check if the folder temp

exists, and if it exists, delete it

the command to delete a directory is only echoed to the console. If the output is correct, remove the commandecho



Switch /S

on rd


/S      Removes all directories and files in the specified directory
        in addition to the directory itself.  Used to remove a directory


This does not mean that it will search all directories that look for a name with the specified name and delete them.

In other words, if you run rd /S Test

from a folder C:\Temp

, it will remove C:\Temp\Test\*.*

, including all subdirectories (of any name) C:\Temp\Test

. It doesn't mean it will delete C:\Temp\AnotherDir\Test

, because it is not a subfolder C:\Temp\Test




All Articles