title: Windows batch script to rename and delete files based on extension and time created
body: <p>We have a program that take specific XML files and imports them, then changes the file from .xml to .tmp. Once it turns to .tmp we can delete the file.</p>
  
  <p>Occasionally what happens is the XML file doesn't import properly, in which case it gets renamed from .xml to .bad and the file has to be manually adjusted (could be an improper xml file or whatever). What we've discovered is that roughly 90% of the time if you rename the file back to .xml it imports fine.</p>
  
  <p>I've created a batch script to run every couple of minutes that automatically deletes the .tmp files. What I also want to do is add something to the script that renames .bad file to .xml in order to let the program try and import it again. That will cover the 90%.</p>
  
  <p>In order to cover the last 10%, I'd like to set it up so that after about 10 minutes, it no longer tries to rename the file .bad back to .xml (based on the creation date).</p>
  
  <p>So far, what I have is the script to delete the .tmp files:</p>
  
  <pre><code>forfiles -p "C:\path\to\xml" -m *.tmp -c "cmd /c del @PATH"
  </code></pre>
  
  <p>And I have the script to rename .bad to .xml:</p>
  
  <pre><code>forfiles -p "C:\path\to\xml" -m *.bad -c "cmd /c ren *.bad *.xml"
  </code></pre>
  
  <p>How do I tell the 2nd command to do it only for files whose creation date (not last modified date) was less than 10 minutes ago?</p>
  
  <p><strong>Updated:</strong> </p>
  
  <p>Here's the full solution (thanks to jon Z for pointing me in the right direction):</p>
  
  <pre><code>powershell.exe -command "get-childitem 'path\to\xml' -filter *.tmp | Remove-Item"
  
  powershell.exe -command "get-childitem 'path\to\xml' -filter *.bad | where-object {$_.creationtime -gt (get-date).addminutes(-8)} | foreach-object {move-item $_.fullname ($_.fullname-replace '.bad','.xml')}"
  </code></pre>