3

I want to use windows command line to remove all files and directories except one, the folder ".svn". I tried doing it like this, in two steps (my working directory is the folder that I want to clean):

  1. First remove all directories with the exception of ".svn": dir /B /A:D | findstr /V ".svn" | rmdir /Q
  2. Remove all files: del * /F /Q

Step 2 is OK, but for step 1 I get an "The syntax of the command is incorrect." error. Thrown by the rmdircmd. Does anybody know how to do this OK: delete all directories except one.

Nick V
  • 1,793
  • 2
  • 17
  • 19
  • 1
    http://stackoverflow.com/questions/3008567/windows-batch-script-to-delete-everything-in-a-folder-except-one – Flot2011 Mar 15 '12 at 10:44
  • http://stackoverflow.com/questions/8281289/dir-list-all-folders-except-some – Aacini Mar 16 '12 at 15:03
  • So you redirect the dir-output to findstr? Yuck! What do you try to accomplish with the second Pipe after Findstr? This cannot work. See my answer below. – Dlanod Kcud Aug 31 '16 at 08:14

1 Answers1

0
for /f "tokens=*" %i in ('dir /B /A:D') do if ["%i"] neq [".svn"] rd "%i" /f /q

explanation

the for-loop enumerates the output of the dir-command. you must specify tokens=* in case there are spaced in the directory name.

The square brackets and double quotes around the if-parameters is an old trick to avoid problems if the argument ever gets nothing (or blank) or contains blanks

Dont forget to double the %-signs if you want to put this in a cmd-file

Dlanod Kcud
  • 517
  • 5
  • 13