How to delete all files except current date files in SSIS Package - SSIS Tutorial


You are working as ETL Developer or an SSIS developer, you need to write an SSIS Package that should be able to delete all the old files from a folder and just leave the current day files.

How to Delete all files from a folder except Files created today in SSIS Package by using Script Task


We can get the Date Created for the file and if it is created today we will not delete , otherwise we will delete the file.

Step 1: Create an SSIS Package by using SSDT

Step 2: Create a variable called SourceFolder and provide the path for folder in which your files exists.
How to drop all old files from folder just leaving for current day files in SSIS Package by using Script Task

Step 3: 
Bring the script Task to the Control Flow pane and add SourceFolder variable to it as shown below.
How to drop old files from a folder by using SSIS Script Task except Current Day files

Step 4 : 
Click on Edit Script and then use the page the below script.

Click Edit Button and it will open Script Task Editor.
Under #region Namespaces, I have added below code

using System.IO;

Go to public void Main() and Paste below Code right under // TODO: Add your code here

           var directory = new DirectoryInfo(Dts.Variables["User::SourceFolder"].Value.ToString());
            FileInfo[] files = directory.GetFiles();
            foreach (FileInfo file in files)
                if (file.CreationTime < DateTime.Today)

Step 5:
Save the script and then close the Editor window. Your SSIS Package is ready. Execute and it should delete all the old files from a folder and leave only current day files. Noticed, my SSIS Package has deleted all the files and I am left with only current day files in my folder.

Delete all files except current day files in SSIS Package by using Script Task