When it comes to automating IT-related tasks, batch scripting is the best choice. And if you are using Windows you have really good alternatives like python, cmd (dos) and Powershell. In this post, I will show you how to delete empty folders and subfolders using CMD.

CMD is the new slang for Dos lately. Because it is launched using cmd command on Windows Run box. Using cmd we can automate many repetitive tasks easily.


Script to Delete Empty Folders with Subfolders

If you want to delete empty folders along with all subfolders recursively, copy the following script and save it as deleteblank.bat into the folder you want to clean up.

</ins>
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i" 2>NUL

After you have saved the file, simply run this script by double-clicking on the batch file. You need admin rights to run this kind of scripts on your machine. If you have enough rights, the script will run and delete all empty folders under that directory.


If you are into Scripting, you may like other scripts I have shared under Scripting category. I also have a few Python and Powershell scripts. If you like this script or need any assistance, please share your comments in the discussion link below.