After using some of the ways to delete the files from the particular folder like Delete file using Script Task and File System Task in SSIS. We have seen such methods and used logic to get the files from the loop container and process to delete them and same thing applied for the scripts as well. we also looked for the files deletion which are older than some Retention period with Script Task also.
In earlier posts we have not used any parameters in the scripts and directly applied folder path and retention period values there. But here i would like to use parameters for the folder path and retention period and pass through the scripts and delete them as per condition.
1. Let check the files from the target folder.
2. Create parameters and set the values. Here it is going to be delete the files which are older then 3 days from the E:\ImagesBackup folder in this case.
3. Drag and drop File System Task
5. Put the parameters as ReadOnlyVariables
6. Apply attached script in editor which have additional logic with condition to check the file lat modified date and check if older than specified retention period or not. Here you can see the parameters used in the script.
Please note here we need to import system.IO namespace.
7. Turn on final step and run package. Files older than specified retention period get deleted.
You can use the script below for the same as mentioned in above image,
In earlier posts we have not used any parameters in the scripts and directly applied folder path and retention period values there. But here i would like to use parameters for the folder path and retention period and pass through the scripts and delete them as per condition.
1. Let check the files from the target folder.
2. Create parameters and set the values. Here it is going to be delete the files which are older then 3 days from the E:\ImagesBackup folder in this case.
3. Drag and drop File System Task
5. Put the parameters as ReadOnlyVariables
6. Apply attached script in editor which have additional logic with condition to check the file lat modified date and check if older than specified retention period or not. Here you can see the parameters used in the script.
Please note here we need to import system.IO namespace.
7. Turn on final step and run package. Files older than specified retention period get deleted.
You can use the script below for the same as mentioned in above image,
--// You need to apply below one line in "namespaces" region.
using System.IO;
--//You need to apply below lines inplace of // TODO: Add your code here
int RetentionPeriod = Convert.ToInt32(Dts.Variables["User::Period"].Value.ToString());
string directoryPath = Dts.Variables["User::BackupFolder"].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.*");
foreach (string currFile in oldFiles)
{
FileInfo currFileInfo = new FileInfo(currFile);
if (currFileInfo.LastWriteTime < (DateTime.Now.AddDays(-RetentionPeriod)))
{
currFileInfo.Delete();
}
}
Hope you liked this post. Stay tuned for more. 





This is good article but i don't think it will work for SSIS\SQL Server 2005.
ReplyDelete