JSI Tip 0617 - How do I remove all files and sub-directories from a folder, without removing the folder?

Jerold Schulman

July 26, 1998

1 Min Read
ITPro Today logo in a gray background | ITPro Today

Typing RD /S /Q FolderName will delete all files, sub-directories and the FolderName also. If you wish to not remove the target folder, create DELTREE.BAT in your path:

 @echo offif {%1}=={} @echo Syntax: DelTree Folder&goto :EOFif not exist %1 @echo Syntax: DelTree Folder - Folder %1 does not exist.&goto :EOFpushd %1if %ERRORLEVEL% NEQ 0 @echo Syntax: DelTree Folder - Folder %1 does not exist.&goto :EOFdel /q /f "*.*"for /f "Tokens=*" %%i in ('dir %1 /B /A /AD') do rd /s /q "%%i"popd

To delete all the files and sub-directories in My Test Folder, type:

DELTREE "My Test Folder"



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like