Scenario:
You want to create a package that will read the folder Path,FileType,FileNameLike and Retention Period from Some Table and Then loop through each Folder path and delete required files those rentention period is more than defined.
Solution:
Step1: Lets create table with some data
CREATE TABLE [dbo].[PurgeInformation]
(
[ID] [INT] IDENTITY(1, 1) NOT NULL,
[FolderPath] [VARCHAR](300) NULL,
[FileType] [VARCHAR](20) NULL,
[RetentionPeriod] [INT] NULL,
[FileNameLike] [VARCHAR](100) NULL
)
ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[PurgeInformation] ON
GO
INSERT [dbo].[PurgeInformation]
([ID],
[FolderPath],
[FileType],
[RetentionPeriod],
[FileNameLike])
VALUES (1,
N'C:\SSISTraining\HDW',
N'.txt',
5,
N'Log')
GO
INSERT [dbo].[PurgeInformation]
([ID],
[FolderPath],
[FileType],
[RetentionPeriod],
[FileNameLike])
VALUES (2,
N'C:\SSISTraining\SourceFiles',
N'.xls',
4,
N'Pkg_Log')
GO
SET IDENTITY_INSERT [dbo].[PurgeInformation] OFF
GO
Step2: Create SSIS Package and Create a variable VarObject type object.
Step3: Use Execute SQL Task to read the values from dbo.PurgeInformation table and load into VarObject variable as show in pictures
![]()
![]()
Step4: Drag Foreach loop and configure as show in pictures
![]()
![]()
Step5: After configurint Foreach Loop, Drag Script task inside Foreach loop and Select required variable as show in picture
![]()
Step6: After selecting variable , click on Edit Script,paste the below script
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_e08fb84fbe344cddb17bf307ee516216.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, do something like the following:
bool bFireAgain = true;
Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, ref bFireAgain);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
string FileDirectory, FileNamePattern, FileType;
int RetentionPeriod;
FileDirectory=Dts.Variables["User::VarFolderPath"].Value.ToString();
FileType=Dts.Variables["User::VarFileType"].Value.ToString();
FileNamePattern = Dts.Variables["User::VarFileNameLike"].Value.ToString();
RetentionPeriod =Convert.ToInt32(Dts.Variables["User::VarRetentionPeriod"].Value.ToString());
var files = new DirectoryInfo(FileDirectory).GetFiles(FileNamePattern + "*" + FileType);
// MessageBox.Show("OUT of Loop");
foreach (var file in files)
{
// MessageBox.Show(" I AM IN LOOP");
if (file.CreationTime < taskresult =" (int)ScriptResults.Success;" href="http://2.bp.blogspot.com/-O1DDgjZ92RE/TWNQmtP2xlI/AAAAAAAACws/tZDjvg_5RMQ/s1600/Purge6.png">![]()
You can schedule your package by SQL Server Agent nightly or can run on demand to delete files from different folder with different retention period.
You can add new folder path in table to delete required files.
You want to create a package that will read the folder Path,FileType,FileNameLike and Retention Period from Some Table and Then loop through each Folder path and delete required files those rentention period is more than defined.
Solution:
Step1: Lets create table with some data
CREATE TABLE [dbo].[PurgeInformation]
(
[ID] [INT] IDENTITY(1, 1) NOT NULL,
[FolderPath] [VARCHAR](300) NULL,
[FileType] [VARCHAR](20) NULL,
[RetentionPeriod] [INT] NULL,
[FileNameLike] [VARCHAR](100) NULL
)
ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[PurgeInformation] ON
GO
INSERT [dbo].[PurgeInformation]
([ID],
[FolderPath],
[FileType],
[RetentionPeriod],
[FileNameLike])
VALUES (1,
N'C:\SSISTraining\HDW',
N'.txt',
5,
N'Log')
GO
INSERT [dbo].[PurgeInformation]
([ID],
[FolderPath],
[FileType],
[RetentionPeriod],
[FileNameLike])
VALUES (2,
N'C:\SSISTraining\SourceFiles',
N'.xls',
4,
N'Pkg_Log')
GO
SET IDENTITY_INSERT [dbo].[PurgeInformation] OFF
GO
Step2: Create SSIS Package and Create a variable VarObject type object.
Step3: Use Execute SQL Task to read the values from dbo.PurgeInformation table and load into VarObject variable as show in pictures


Step4: Drag Foreach loop and configure as show in pictures


Step5: After configurint Foreach Loop, Drag Script task inside Foreach loop and Select required variable as show in picture

Step6: After selecting variable , click on Edit Script,paste the below script
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_e08fb84fbe344cddb17bf307ee516216.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, do something like the following:
bool bFireAgain = true;
Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, ref bFireAgain);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
string FileDirectory, FileNamePattern, FileType;
int RetentionPeriod;
FileDirectory=Dts.Variables["User::VarFolderPath"].Value.ToString();
FileType=Dts.Variables["User::VarFileType"].Value.ToString();
FileNamePattern = Dts.Variables["User::VarFileNameLike"].Value.ToString();
RetentionPeriod =Convert.ToInt32(Dts.Variables["User::VarRetentionPeriod"].Value.ToString());
var files = new DirectoryInfo(FileDirectory).GetFiles(FileNamePattern + "*" + FileType);
// MessageBox.Show("OUT of Loop");
foreach (var file in files)
{
// MessageBox.Show(" I AM IN LOOP");
if (file.CreationTime < taskresult =" (int)ScriptResults.Success;" href="http://2.bp.blogspot.com/-O1DDgjZ92RE/TWNQmtP2xlI/AAAAAAAACws/tZDjvg_5RMQ/s1600/Purge6.png">

You can schedule your package by SQL Server Agent nightly or can run on demand to delete files from different folder with different retention period.
You can add new folder path in table to delete required files.