Friday, September 25, 2009

delete old files with a batch script

Here's some VBS code that will do what you want.  First, you will need to setup a scheduled task and run it daily.  Just change the number to the amount of days you want to keep file in that directory.  Any files in the directory will be deleted.  This file must be place WITHIN the directory you wish to clean.

'First, you will need to setup a scheduled task and run it
'daily.  Just change the number to the amount of days you
'want to keep file in that directory.  Any files in the
'directory will be deleted.  This file must be place WITHIN
'the directory you wish to clean.
'Courtesty of monsterjta @ tek-tips.com

'Start
'Delete any file in current directory that is older than iDaysOld

Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld
Dim CurDir

'Definitions
iDaysOld = 5
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = CreateObject("WScript.Shell").CurrentDirectory
sDirectoryPath = sDirectory & ""
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files

'Walk through each file in this folder collection.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next

'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
'End

Source: http://www.tek-tips.com/viewthread.cfm?qid=1301870 Print Friendly and PDF
Share/Bookmark

No comments:

Post a Comment