title: How can I delete all files/subdirs except for some files in DOS?
body: <p>I'm looking for a DOS script to delete all files and subdirectories in a root directory except for a set of batch files (*.bat) that are in the root directory.  Any DOS jocks out there that know an easy way to do this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks for your help everyone.  This is where I'm at now (see below).  I'm using Ken's suggestion for the delete of the files.  I would like to know how I can stop this script running if the <code>del</code> or <code>RD</code> commands fail due to a lock on a file/dir.  Anyone know how?  Right now, this script will do a bunch of things after the deletes and I'd like to stop the script if any of the deletes fail.</p>
  
  <pre><code>@echo off
  
  REM *********************************************************************
  REM *  Delete all files and subdirs except for batch files in the root  *
  REM *********************************************************************
  
  REM Delete all files in current dir except bat files.  Does this by a) setting the attributes of *.bat files to 
  REM readonly and hidden, b) deleting the rest, c) reseting the attributes 
  
  attrib +r +s *.bat
  del *.* /S /Q
  attrib -r -s *.bat
  
  REM Deletes ALL subdirectories 
  
  FOR /D  %%G in (*) DO RD /s /q %%G
  </code></pre>