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.

Add to favorites
E-mail Twitter Digg Facebook
  • 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
            End If
          End If
        For Each oSubfldr In oFldr.SubFolders
          PurgeFolder oSubfldr
      End Sub
    • I wish the discussion editor would preserve line breaks . . .