Script Center > Gallery > Storage > Delete All the Files in a Folder Older Than 7 Days
TechNet Script Center logo

Welcome to the TechNet Script Center Gallery!

Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.

Delete All the Files in a Folder Older Than 7 Days

(Community)
Rate it:
 
 
 
 
 
Script Code
VBScript
'**************************************************************************
'* FolderCleaner.vbs
'*
'* This script will delete all files older than a specified number of days
'* from the specified folder.
'*
'* It accepts no arguments and outputs a logfile C:\ReportCleaner.log
'*
'* Created 29 September 2003 by Chris Bennell
'*
'**************************************************************************


strfolder = "c:\tereports" 'The folder to delete files from
intAgeToDelete = 7 ' The age (in days) to delete files if equal or older.

intDeletedCount = 0
LogFilename = "C:\ReportCleaner.log"
Set fso = CreateObject("Scripting.FileSystemObject")
Set OutputFile = FSO.CreateTextFile(LogFilename,True)
Set WshNetwork = WScript.CreateObject("WScript.Network") 'declaration of a network object for storing computer name
Set checkfolder = fso.GetFolder(strfolder)

computer = WshNetwork.ComputerName

OutputFile.write WScript.ScriptName & " script run on " & computer & ", date: " & NOW()
OutputFile.write vbCrlf & "This script will delete all files equal to or older than " & intAgeToDelete & " days in the " & strfolder & " folder."
CheckFiles(checkfolder) 'Check all files in the folder and delete old ones.
OutputFile.write vbCrlf & intDeletedCount & " files deleted."
OutputFile.write vbCrlf & "********** End of ReportCleaner log **********"
OutputFile.Close
Set fileObject = Nothing
Set fso = Nothing

'----------------------------
Sub CheckFiles(Folder)
'This procedure simply passes all files in a folder to the DeleteFiles procedure
    For Each File In Folder.Files
      Checkstring = File.Path
      DeleteFiles(Checkstring)
      If err.number <> 0 Then OutputFile.Write vbCrlf & err.description
    Next
End Sub
'----------------------------

'----------------------------
Sub DeleteFiles(strfile)
'This procedure checks the date a file was created and deletes it if is older than a specfied number of days
  Set fileObject = fso.GetFile(strfile)
'  wscript.echo fileObject.Name
'  wscript.echo fileObject.DateCreated

  DaysOld = Round(now() - fileObject.DateLastModified)
  wscript.echo fileObject.Name & " created " & fileObject.DateLastModified & " is " & DaysOld & " days old."
  If DaysOld >= intAgeToDelete Then   
    OutputFile.write vbCrlf & fileobject.name & " was created earlier than the specified number of days"
'   Wscript.Echo "Deleted "& fileObject.path & fileObject.name
    OutputFile.write vbCrlf & "Deleted "& fileObject
    fso.DeleteFile(fileObject)
    intDeletedCount = intDeletedCount +1
  End If
End Sub
'----------------------------
Platforms
Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 No
Windows 7 No
Windows Vista No
Windows XP No
Windows 2000 No
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.