title: How to delete list of files in batch file
body: <p>I'm attempting to delete all non-code files from directories and their subdirectories using a batch script. Using the command prompt I can verify the results of the script, but when the script is executed it does not perform the actions one would expect.</p>
  
  <pre><code>for /F %%F in ('dir /s /b /a-d ^| findstr /vile ".c .cpp .h .hpp"') do if "%%~fF" neq      "%~f0" del /S /Q "%%F"
  </code></pre>
  
  <p>When extracting and executing the following command, the paths produced are correct:</p>
  
  <pre><code>dir /s /b /a-d | findstr /vile ".c .cpp .h .hpp"
  </code></pre>
  
  <p>However, when executing it via the batch file the resulting paths sent to the 'del /S /Q "%%F"' command are the first subdirectory over and over.</p>
  
  <p>The following is a comparison of the command prompt output and the batch file output (with the del command replaced by echo)</p>
  
  <p>Console:</p>
  
  <pre><code>C:\testdata\delete_test&gt;dir /s /b /a-d | findstr /vile ".c .cpp .h .hpp"
  C:\testdata\delete_test\delete_non_code_files.bat
  C:\testdata\delete_test\delete_test - Copy\7328_log.txt
  C:\testdata\delete_test\delete_test - Copy\7588_log.dat
  C:\testdata\delete_test\delete_test - Copy\7964_log.txt
  C:\testdata\delete_test\delete_test - Copy\8860_log.txt
  C:\testdata\delete_test\delete_test - Copy (2)\7328_log.txt
  C:\testdata\delete_test\delete_test - Copy (2)\7588_log.dat
  C:\testdata\delete_test\delete_test - Copy (2)\7964_log.txt
  C:\testdata\delete_test\delete_test - Copy (2)\8860_log.txt
  C:\testdata\delete_test\delete_test - Copy (3)\7328_log.txt
  C:\testdata\delete_test\delete_test - Copy (3)\7588_log.dat
  C:\testdata\delete_test\delete_test - Copy (3)\7964_log.txt
  C:\testdata\delete_test\delete_test - Copy (3)\8860_log.txt
  </code></pre>
  
  <p>Batch file:</p>
  
  <pre><code>C:\testdata\delete_test&gt;delete_non_code_files.bat
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  /S /Q "C:\testdata\delete_test\delete_test"
  </code></pre>
  
  <p>Why doesn't my %%F variable point to a file as 'for /F' should cause it to do?</p>