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

Scenario:

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


Solution:

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)
                {
                    //MessageBox.Show(file.Name);
                   file.Delete();
                }
            }

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

3 comments:

  1. Maha Board 10th Model Paper
    Maharashtra State Board Of Secondary And Higher Secondary Education (MSBSHSE) has Going to Conducted High School Final Exam 2023, Maha Board 10th Model Paper Maharashtra High School Exam Very year Month of April, This Exam Every Year More Than 50 laks of Students Appeared, MSBSHSE Announce 6th, 7th, 8th, 9th, 10th Class Final Exam Date Sheet Available at Official Website at www.mahahsscboard.maharashtra.gov.in.

    ReplyDelete
  2. The offset voltage will make a blunder in the applied upgrade a gadget under test (DUT) or the worth estimated by the voltmeter.عالية الجهد مكثف السيراميك

    ReplyDelete
  3. Electrician may need to install and maintain electrical systems for wastewater treatment plants and other municipal facilities.

    ReplyDelete