Delete Files From A Particular Folder in Script Task using C# Code


Step 1
Create  a SSIS variable "Folder_Path" and  assign the value of file path
Step 2
Use Script Task
Step 3
Read the variable in ReadOnlyVariables Pane.


Step 4
Add the following code

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;

namespace ST_21af426c5ff24863a648aab041051234.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

       

      
 //To Delete the files from folder
        public void Main()
        {
         String Folder = Dts.Variables["Folder_Path"].Value.ToString(); // Path
            Array.ForEach(Directory.GetFiles(Folder), delegate(String path) { File.Delete(path); });
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }

}

No comments:

Post a Comment