VBScript: Delete Old Files and Folders
Here is a simple VBScript that you can schedule to run nightly that will clean out files and folders in a selected path older than a specified number of days. Maybe you have a shared temporary working space for documents or a location where scanned or faxed documents are dropped. This VBS script will ensure that these locations don't get clogged up or overly cluttered. Read further for the code...
The following code will get rid of old files and folders. Just put in the path and how old the files or folders need to be before they are considered stale and should be removed.
'* Script Name: DeleteOldFiles.vbs
'* Created On: 12/07/2007
'* Author: Michael C. Panagos
'* Website: http://www.grimadmin.com
'* Purpose: Delete files & folders older than x days
'* History: Michael C. Panagos 12/07/2007
'* Initial Draft.
'* Legal: Copying and distribution of this code, with or without modification,
'* are permitted in any medium without royalty provided the copyright
'* notice and this notice are preserved. This code is offered as-is,
'* without any warranty.
'Account used to run script needs delete permissions to folder & files.
'Set the following variables
TempFolderPath = "X:\YourFolder\YourSubfolder" 'no trailing backslash
NumberOfDays = 60 'anything older than this many days will be removed
'Set objects & error catching
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(TempFolderPath)
'DELETE all files in TempFolder Path older than x days
For Each objFile In objFolder.files
If DateDiff("d", objFile.DateCreated,Now) > NumberOfDays Then
'DELETE all subfolders in TempFolder Path older than x days
For Each objSubfolder In objFolder.Subfolders
If DateDiff("d", objSubfolder.DateCreated,Now) > NumberOfDays Then
Tag: vbscript file management