Monday 30 May 2011

delete old bakcup files for a particular folder (we can create job using this)

run as :  sql agent server account




Option Explicit
on error resume next
        Dim oFSO
        Dim sDirectoryPath
        Dim oFolder
        Dim oFileCollection
        Dim oFile
        Dim Message       
        Dim iDaysOld
        Dim oFSFile
        Dim objTextFile
        Dim objFSO
        Const ForAppending = 8


       
        'Customize values here to fit your needs
        iDaysOld = 2
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        sDirectoryPath = "T:\MSSQL.2\MSSQL\Backup\Efpdb1"
        set oFolder = oFSO.GetFolder(sDirectoryPath)
        set oFileCollection = oFolder.Files
        Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set objTextFile = objFSO.OpenTextFile _
                                            ("T:\MSSQL.2\MSSQL\Backup\delete_Efpdb1Backup.txt", ForAppending, True)
                                   
               
'Walk through each file in this folder collection.
'If it is older than 2 days, then delete it.
        For each oFile in oFileCollection
                If oFile.DateLastModified < (Date() - iDaysOld) Then
                             objTextFile.WriteLine Date & " " & Time & " Deleting File " & oFile.name
                                 oFile.Delete(True)
                End If
        Next

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

No comments:

Post a Comment