Justin Self
Justin Self

Reputation: 6265

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?

Upvotes: 3

Views: 6720

Answers (2)

Arun
Arun

Reputation: 2523

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)

Upvotes: 7

Anthony Miller
Anthony Miller

Reputation: 15921

<3 for loops

FOR /F "USEBACKQ tokens=*" %%F IN (`dir /b /a:d /s "C:\top\directory\" ^| FIND /v /i "C:\directory\to\omit"`) DO (
 rmdir /s "%%F"
)

and if you wish to strike dangerously, use the /q switch w/ rmdir 0.o

So lets say, you want to perform a remdir /s on C:\Documents and Settings\Mechaflash\, HOWEVER you wish to keep the .\Mechaflash folder (emptied)

FOR /F "USEBACKQ tokens=*" %%F IN (`dir /b /a:d /s "C:\Documents and Settings\Mechaflash\" ^| FIND /v /i "C:\Documents and Settings\Mechaflash\"`) DO (
 rmdir /s "%%F"
)
DEL /Q /F "C:\Documents and Settings\Mechaflash\*"

Upvotes: 1

Related Questions