title: Keep files newer than latest 10
body: <p>I'm writing a VBScript which will be deleting files with extension: '.bac' older than top 10.
  I currently have written code to delete files older than 5 days and only if in the folder are more than 12 files but it delete all old files.
  My target is to keep top 10 files with extension: '.bac' even if they are older than 5 days, because they are backup of my database (one is copy of configuration base and the second one is copy of product base, so 5 files need to be config backups and 5 files need to be product backups).</p>
  
  <pre><code>Option Explicit
  On Error Resume Next
  Dim oFSO, oFolder, sDirectoryPath
  Dim oFileCollection, oFile, sDir
  Dim iDaysOld
  
  'Specify Directory Path From Where You want to clear the old files
  sDirectoryPath = "D:\Backup"
  
  'Specify Number of Days Old File to Delete
  iDaysOld = 5
  
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  Set oFolder = oFSO.GetFolder(sDirectoryPath)
  Set oFileCollection = oFolder.Files
  
  '***********************BG File count*************************
  Dim strDirectory, nThreshold, counter, extension, msgtext
  Dim objFSO, objFolder, objFile, Logfile
  
  'file extension to look for
  extension = "bac"
  
  'directory to look in
  strDirectory = "D:\Backup"
  
  counter = 0
  
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFSO.GetFolder(strDirectory)
  
  For Each objFile In objFolder.Files
    If LCase((objFSO.GetExtensionName(objFile))) = LCase(extension) Then
      counter = counter + 1
    End If
  Next
  '***********************End of file count*********************
  
  If counter &gt; 12 Then
    For each oFile in oFileCollection
      'This section will filter the text file as i have used for for test
      'Specify the Extension of file that you want to delete
      'and the number with Number of character in the file extension
  
      If LCase(Right(Cstr(oFile.Name), 3)) = "bac" Then
        If oFile.DateLastModified &lt; (Date() - iDaysOld) Then
          oFile.Delete(True)
        End If
      End If
    Next
  End If
  Set oFSO = Nothing
  Set oFolder = Nothing
  Set oFileCollection = Nothing
  Set oFile = Nothing
  </code></pre>