Windows – How to delete files and subdirectories from folder but not that folder itself

command linewindows

What commands can I use (from the command prompt) to delete all files and all subdirectories from a folder, but not delete the folder itself? Basically at the end of the delete, there should be an empty folder.

Best Answer

cd <foldername>
del *.*

will delete the files. You'll need to do

rmdir /s <subfolder>

for each subfolder.

Update

Try this in a batch file:

@echo off
cd "%1"
del *.* /y
for /d %%i in (*) do rmdir /s /q "%%i" 

Call it something like EmptyDir.bat. Then you can type:

emptydir <dirname>

and it will delete the files and folder in that folder, but leave the folder there.

Related Question