none
Delete files older than a month using C# in SSIS RRS feed

  • Question

  • Delete the files in a specific folder older than a months using SSIS Script Task (C#).

    Folder has files with different name.

    Just based upon the creation time files should be deleted.


    Monday, February 11, 2013 9:25 PM

Answers

  • You could use System.IO and C# like this:

    string[] files = Directory.GetFiles(@"C:\SomeFolder");
    foreach (string file in files)
    {
         if (File.Exists(file))
         {
              if (File.GetCreationTime(file) 
                  < DateTime.Now.AddDays(-30))
              {
                  File.Delete(file);
              }
         }
    }


    Tuesday, February 12, 2013 2:00 AM

All replies

  • here is an example with file dates
    http://microsoft-ssis.blogspot.com/2011/01/use-filedates-in-ssis.html

    Please mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter

    Monday, February 11, 2013 9:34 PM
    Moderator
  • You could use System.IO and C# like this:

    string[] files = Directory.GetFiles(@"C:\SomeFolder");
    foreach (string file in files)
    {
         if (File.Exists(file))
         {
              if (File.GetCreationTime(file) 
                  < DateTime.Now.AddDays(-30))
              {
                  File.Delete(file);
              }
         }
    }


    Tuesday, February 12, 2013 2:00 AM
  • Thanks for the soluition :)

    Tuesday, February 12, 2013 8:09 PM
  • Thanks for the soluition :)

    Tuesday, February 12, 2013 8:10 PM
  • Hi my directory is stored in my SSIS in the ReadOnlyvariables as  $Project::VerifyAddressFileOutputDestination

    My code is written as follows. I need help in the underlined syntax from below. Kindly help. 

    public void Main()
    {
    // TODO: Add your code here
                string[] files = Directory.GetFiles(@"[$Project::VerifyAddressFileOutputDestination]");
                foreach (string file in files)
                {
                    if (File.Exists(file))
                    {
                        if (File.GetCreationTime(file) < DateTime.Now.AddDays(-365))
                        {
                            File.Delete(file);
                        }
                    }
                }
                Dts.TaskResult = (int)ScriptResults.Success;
            }

    Thursday, July 2, 2020 4:11 PM
  • Hi Ravichandran Rajagopal,

    You would need to modify the line in question as follow:

    string[] files = Directory.GetFiles(Dts.Variables["$Project::VerifyAddressFileOutputDestination"].Value.ToString());

    And it is better to use the following technique for a month. Because a month could have 28, 29, 30, or 31 days.

    DateTime.Now.AddMonths(-1)


    Thursday, July 2, 2020 4:42 PM