Search code examples
batch-filecmdwindows-console

Use "rmdir" functionality but keep specified directory


I would like the functionality of rmdir /s but I need to keep the specified directory. rmdir /s removes all files and sub directories in addition to the directory specified.

I've also tried using del /s but then I am left with empty folders in the specified directory. I need those folders removed as well.

Any guidance on how I can do this?


Solution

  • Easiest way would be to change directory to specified directory and invoke an rd command on the "." directory. Like:

    cd toYourDirectory (or pushd toYourDirectory)
    rd /q /s . 2> nul
    
    • /q - ensures you wont be prompted
    • /s - to do subfolders, files, so on..
    • the "." - implies CURRENT directory
    • 2>nul - ensures it won't report the error when the rd command attempts to remove itself (which is what you want)