title: Write batch file to delete all but latest X files of each type
body: <p>I am trying to create a batch file which should be run once a day, which is supposed to delete all backup files other than the latest, lets say 2 of each type. So I am having files such as:</p>
  
  <ul>
  <li>backup-DAILY-2018-11-13_000000.bak</li>
  <li>backup-DAILY-2018-11-12_000000.bak</li>
  <li><p>backup-DAILY-2018-11-11_000000.bak</p></li>
  <li><p>backup-HOURLY-2018-11-13_110000.bak</p></li>
  <li>backup-HOURLY-2018-11-13_130000.bak</li>
  <li>backup-HOURLY-2018-11-13_150000.bak</li>
  </ul>
  
  <p>etc.</p>
  
  <p>So my script looks as follows currently</p>
  
  <pre><code>@ECHO off
  
  SET frequency=%*
  for %%x in (%frequency%) do (
    for /f "*-%%x*" %%a in ('dir /ad /b /o-d^|more +2') do echo @file
  )
  </code></pre>
  
  <p>but I am getting the error <code>*-%x*" was unexpected at this time.</code></p>
  
  <p>What the script should achieve is that once it is run each day I should have the latest 2 backup files of each type(DAILY, HOURLY). Right now I am just trying to print the files that need to be kept.</p>
  
  <p>So what I should be left with is </p>
  
  <ul>
  <li>backup-DAILY-2018-11-13_000000.bak</li>
  <li><p>backup-DAILY-2018-11-12_000000.bak</p></li>
  <li><p>backup-HOURLY-2018-11-13_130000.bak</p></li>
  <li>backup-HOURLY-2018-11-13_150000.bak</li>
  </ul>