title: Batch delete all files and directories except specified file and directory
body: <p>I'm trying to delete all files and directories in a specific directory using a bat file in said directory. I've seen this done on Linux without problem, but in Windows command environment it seems to be a chore.</p>
  
  <p>Example: \temp\1.bat (keep) \temp\special folder (keep) </p>
  
  <p>Inside \temp\ contains all the folders and files I want to delete except the 1.bat and the special folder. </p>
  
  <p>I have tried recursive commands but they delete the directory, or they delete all the files and keep the directories.</p>
  
  <p>Example: </p>
  
  <pre><code>attrib +r "special directory"
  attrib +r "1.bat"
  erase /Q *.*
  rd /s /q
  attrib -r "1.bat"
  </code></pre>
  
  <p>But this remove everything. If I remove the <code>rd</code> line, it removes all the files, not the directories and keeps the 1.bat file (like I need).</p>
  
  <p>I've also tried:</p>
  
  <pre><code>for /d %%i in (".\*") do if /i not "%%i"=="special folder" rd /s /q "%%i"
  </code></pre>
  
  <p>But this doesn't work either. I simply cannot get all the directories and files to be deleted except for the "special folder" and the "1.bat file".</p>
  
  <p>Is this even possible?</p>