Delete Files More than 14 Days Old

Deletes files older than 14 days. To specify a different time interval, change the value of the variable RetentionPeriod.

 
 
 
 
 
(0)
Add to favorites
Storage
8/10/2009
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Superior Method
    2 Posts | Last post June 02, 2011
    • This script should not be published in this forum because of its poor use of native VB Script date arithmetic functions. Here is a much better way to delete files older than 14 days:
      
      Const sFolder = "F:\Data"
      Const iRetention = 14  'Retention period in days
      Const bDebug = True
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      Set oFolder = oFSO.GetFolder(sFolder)
      
      PurgeFolder oSubFolder
      
      Sub PurgeFolder(oFldr)
        For Each oFile In oFldr.Files
          If DateDiff("d", oFile.DateCreated, Now) > iRetention Then
            If bDebug Then 
              WScript.Echo "Deleting", oFile.Path
            Else
              oFile.Delete
            End If
          End If
        Next
      
        For Each oSubfldr In oFldr.SubFolders
          PurgeFolder oSubfldr
        Next
      End Sub
    • I wish the discussion editor would preserve line breaks . . .