Search code examples
ssisssis-2012

can ssis pick up the file with the latest date creation?


I am thinking of using ssis reading excel files in the folder.

The folder is updated daily by putting new file in without deleting any old files.

I am now using a 'for each' container to loop all the files and loading them into a consolidated table.

However, the boss only wants the latest file to be loaded into the table and he does not want a incremental table.

Can ssis check the file creation date using some functions in ssis and only load the latest one?


Solution

  • ou can use this script:
    
          public void Main()
             {
    
          // TODO: Add your code here
                 var directory= new DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());
    
                FileInfo[] files = directory.GetFiles();
                DateTime lastModified = DateTime.MinValue;
    
                 foreach (FileInfo file in files)
                {
                    if (file.LastWriteTime > lastModified)
                    {
                        lastModified = file.LastWriteTime;
                        Dts.Variables["User::VarFileName"].Value = file.ToString();
                    }
                }
    
                 MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());
    
    
                 Dts.TaskResult = (int)ScriptResults.Success;
             }
    

    also you can cgeck this link below:

    http://sqlage.blogspot.in/2013/12/ssis-how-to-get-most-recent-file-from.html