Reputation: 53
I am trying to write a simple batch file which will recursively find and delete a folder. But the following script is not looking under sub folder. Wondering how to do that?
@echo off
cd /d "C:\"
for /d %%i in (temp) do rd /s "%%i"
pause
Thanks!
Upvotes: 5
Views: 8954
Reputation: 70923
for /d /r "c:\" %%a in (temp\) do if exist "%%a" echo rmdir /s /q "%%a"
For each folder (/d
), recursively (/r
) under c:\
test for the presence of a temp
folder and if it exist, remove it
directory removal command is only echoed to console. If the output is correct, remove the echo
command
Upvotes: 13
Reputation: 125688
The /S
switch to rd
means
/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.
It does not mean it will search all directories looking for one with the specified name and delete them.
In other words, if you run rd /S Test
from the C:\Temp
folder, it will delete C:\Temp\Test\*.*
, including all subdirectories (of any name) of C:\Temp\Test
. It does not mean it will delete C:\Temp\AnotherDir\Test
, because that isn't a subfolder of C:\Temp\Test
.
Upvotes: 0