Delete Files and Subfolders from Folder but not that Folder Itself

If you would ever want to use a command line delete all the files and folders from within a folder without deleting the folder itself, a batch file can be constructed.

Warning: There are a few things that I discovered while testing this script. Firstly, the CD command, which is part of this script will accept a / character as part of the string and execute. For example, if using CD thisfolder from root then CD will change directory to thisfolder. Also, if using CD /thisfolder from root, then CD will change directory to CD thisfolder. This could be fatal, if this was not your intended folder. Here is an example of that. Most commands seem to have some sort of help built into them. so if you were to type mycommand /h you would get help. However, if with this command, you were to type mycommand /h the command will execute by changing directories to h if it should exist and delete its contents. So, handle with care.

You must have the CD to change directory, otherwise your entire root will be deleted!

I found this source code here.

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

I revised the code to look like this. The main difference is the line for del. There is no /y option at least for Windows 7. The > nul was added to hide the output for consistency, since there is no output for the rmdir.

@echo off
cd %1
del * /s /q > nul
for /d %%i in (*) do rmdir /s /q %%i

Save the file as emptydir.cmd or emptydir.bat.

Usage: emptydir [dirname]