title: batch script delete files when file name does not match list of patterns
body: <p>I want to delete all files in a directory except those whose name matches a certain pattern. A typical example of the kind of files in this directory is as follows:</p>
  
  <ul>
  <li>Bookmarks.xml ---- <em>to delete</em></li>
  <li>DownloadMeta.xml ---- <em>to delete</em></li>
  <li>logfile.log ---- <em>to delete</em></li>
  <li>1745388844.idx ---- <em>to delete</em></li>
  <li>TS1.c.pickle ---- <em>to delete</em></li>
  <li>TS1.prm.bak ---- <em>to delete</em></li>
  <li>!clear.bat ---- <strong>keep</strong></li>
  <li>.gitignore ---- <strong>keep</strong></li>
  <li>BookFlight.c ---- <strong>keep</strong></li>
  <li>CheckItinerary.c ---- <strong>keep</strong></li>
  <li>combined_TS1.c ----<strong>keep</strong></li>
  <li>pre_cci.c ---- <strong>keep</strong></li>
  <li>TS1.prm ---- <strong>keep</strong></li>
  <li>TS1.usr ---- <strong>keep</strong></li>
  <li>vuser_end.c ---- <strong>keep</strong></li>
  <li>vuser_init.c ---- <strong>keep</strong></li>
  <li>globals.h ---- <strong>keep</strong></li>
  <li>ScriptUploadMetadata.xml ---- <strong>keep</strong></li>
  </ul>
  
  <p>i want to keep files with names in a list of patterns:</p>
  
  <pre><code>list_to_ignore = ["!clear.bat", ".gitignore", "*.usr", "default.cfg", 
                      "default.usp", "*.c", "*lobals.h", "*custom_body.h", 
                      "*body_variables.txt", "*loadMetadata.xml", "*.prm" ]
  </code></pre>
  
  <p>For example:</p>
  
  <ul>
  <li><p>file <strong>TS2.c.pickle</strong> does not match any item in the list, because it ends with .pickle extension. It should be deleted.</p></li>
  <li><p>file <strong>somefile.c</strong> matches "*.c" pattern, because it ends with .c. It should be kept.</p></li>
  <li><p>file <strong>Metadata.xml</strong> does not match any pattern, because it lacks prefix <em>"load"</em> at the beginning. It should be deleted.</p></li>
  <li><p>file <strong>Globals.h</strong> matches "<em>lobals.h</em> pattern. It should be kept.</p></li>
  </ul>
  
  <p>Here is what I tried:</p>
  
  <pre><code>@echo off
  FOR /d %%a in ("./*") DO rd "%%a" /q /s
  FOR %%i in (*.*) DO ^
  if not "%%i"=="!clear.bat" ^
  if not "%%i"==".gitignore" ^
  if not "%%i"=="*.usr" ^
  if not "%%i"=="default.cfg" ^
  if not "%%i"=="default.usp" ^
  if not "%%i"=="*.c" ^
  if not "%%i"=="*lobals.h" ^
  if not "%%i"=="*custom_body.h" ^
  if not "%%i"=="*body_variables.txt" ^
  if not "%%i"=="*ploadMetadata.xml" ^
  if not "%%i"=="*.prm" ^
  DEL /s /q "%%i"
  pause
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KQKga.png" rel="nofollow noreferrer">Here is a list of all files in a local directory with check marks</a></p>
  
  <p>Hope this makes sense. If this is not an appropriate question let me know.</p>
  
  <p>Many thanks</p>