November 4, 2009
Some might have noticed that the “del” command will only remove files in a directory and it’s subdirectories.
If you want to delete Subdirectories from a given starting directory (without deleting the root directory itself) you might want to parse through the root folder and delete every directory that is found.
Here’s a small for loop which does the trick.
In this example, all folders within the %temp% folder that start with “OL” would be deleted (Since that is what the “dir” command would return). You might want to modify the path and the wild card to suit your requirements.
In a command line:
for /f %i in ('dir %temp%\OL* /B /D') do rd %i /Q /S
In a batch file:
for /f %%i in ('dir %temp%\OL* /B /D') do rd %%i /Q /S
This post was written by microtom and has 32,047 views in total