1

Currently, I have an SSIS process set up to load the data from a zip file every day. These zip files have a timestamp in the file name.

Example: *data_20201220.gz & *data_20201221.gz

I want to write a script using for loop to delete the files based on the retention period I pass. Example: If I pass 5, it should just keep the most recent 5 files based on the write time.

       // File Retention Period

        int rentenperiod = Convert.ToInt32(Dts.Variables["$Package::pkg_data_files_to_keep"].Value);

        

   // Get the Zip Files

        string Zipfiles = LocalPath + fileName + runDate + ".gz";

        string[] files = Directory.GetFiles(Zipfiles);

        if (files.Length > rentenperiod)
        {            
      for (int i = 0; i < (files.Length - rentenperiod); i = i + 1)
        {
           DateTime FileDate = File.GetLastWriteTime(Zipfiles);
           foreach (string currFile in files)
           {
                if (File.GetLastWriteTime(currFile) == FileDate)
             {
                File.Delete(currFile);
            }}}}
      

Any help would be appreciated.

mickey
  • 11
  • 2

0 Answers0