First add the variables
The folders are strings and the number of days to keep the files are Int32 (integers).
Add a Script task from the Toolbox.
Add the user variables in the ReadOnlyVariables field as follows:-
Click on design script and add the code:-
Option Strict Off
Imports System
Imports System.Data
Imports System.Math
Imports System.IO
Imports Microsoft.SqlServer.Dts.Runtime
Public Class ScriptMain
Public Sub
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld
'Archive Files
iDaysOld = CInt(Dts.Variables("User::DaysKeepArchive").Value) 'Alter the variable DaysKeepArchive as needed.
oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = CStr(Dts.Variables("User::ArchiveFiles").Value) 'Alter the variable ArchiveFiles as needed.
oFolder = oFSO.GetFolder(sDirectoryPath)
oFileCollection = oFolder.Files
'Walk through each file in this folder collection.
'If it is older than DaysKeepArchive days, then delete it.
For Each oFile In oFileCollection
If oFile.DateLastModified < (DateTime.Now.AddDays(-iDaysOld)) Then
oFile.Delete(True)
End If
Next
'Clean up
oFSO = Nothing
oFolder = Nothing
oFileCollection = Nothing
oFile = Nothing
Dts.TaskResult = Dts.Results.Success
End Sub
End Class