Remove all files and directories except one directory
I want to use a windowed command prompt to delete all files and directories except one, the ".svn" folder. I tried to do it like this, in two steps (my working directory is the folder I want to clean up):
- First remove all directories except ".svn":
dir /B /A:D | findstr /V ".svn" | rmdir /Q
- Delete all files:
del * /F /Q
Step 2 is ok, but for step 1 I get "Command syntax is invalid". mistake. Thrown up by the rmdir
cmd command . Does anyone know how to do this OK: delete all directories except one.
source to share
for /f "tokens=*" %i in ('dir /B /A:D') do if ["%i"] neq [".svn"] rd "%i" /f /q
explanation
for-loop lists the output of the dir command. you must specify tokens = * in case there are spaces in the directory name.
Square brackets and double quotes around if parameters is an old trick to avoid problems if the argument never receives anything (or empty) or contains spaces
Don't forget to double% -signs if you want to put this in a cmd file
source to share