0

I am using SSIS to process files in the input folder. I have created a "Script Task" using C# to process the files but it is not working correctly.

I have a input source folder which has two text file, with different "Date modified" timestamp. My problem is I have to get the oldest file that came in last 24 hours first, then the next one. I have written this C# code but it is not picking up the file in correct order.

For example the input folder has these two text files:

FlatFileA 12/19/2021 10:03 AM FlatFileB 12/19/2021 10:04 AM

I have written this code, but it is not picking the files in correct order. I want it to get the "FlatFileA" first and then the next. Here is my code:

public void Main()
{
   string SourceDirectory = Dts.Variables[XPFL_InputDir].Value.ToString();
   string FileName = "";

   var files = new DirectoryInfo(SourceDirectory)GetFiles("*.txt");
   string OldestFile = "";

   foreach (FileInfo file in files)
   {
      if (file.LastWriteTime > DateTime.Now.AddHours(-24))
         {
            OldestFile = file.Name;
         }
   }

   FileName = PathGetFileName(OldestFile);

   MessageBox.Show("Oldest File is " + FileName);

}

The above code is not getting the files in correct order. Can you please take a look at it and let me know what should I do to fix it.

Thanks again for all your help

  • Look at the `if` statement - it says "pick the last file whose `LastWriteTime` is older than 24 hours". Is that what you want? Well, obviously, no. – Camilo Terevinto Dec 19 '21 at 16:20
  • I have to pick the "FlatFileA 12/19/2021 10:03 AM" first then the next one. How should I include the "Ascending" order in my code? Thanks for help. – Alok Singh Dec 19 '21 at 16:43

0 Answers0