0
votes

I want to import the latest csv file into a table using SSIS? I currently have a step that gets the last file in a folder:

 Report_201209030655.csv
 Report_201209030655.csv
 Report_201209030655.csv

Based on created time I want steps to import data of the latest csv to a table.

1
Can`t you just move older/already processed files to separate folder? I believe it's the most common approach in ETL.kyooryu
Before this step I am actually renaming and moving the file to a new folderGallop

1 Answers

0
votes

refer this solution:

[http://blog.sqlauthority.com/2011/05/12/sql-server-import-csv-file-into-database-table-using-ssis/][1]

then use script task to populate the file name and pass that variable as file name for source component.

Getting latest file code:

   public void Main()
        {
            string[] files = System.IO.Directory.GetFiles(@"C:\SSIS\Files");
            DataTable NewList=new DataTable();
            DataColumn col = new DataColumn("FileName");
            NewList.Columns.Add(col);

            System.IO.FileInfo finf;
            foreach (string f in files)
            {
                finf = new System.IO.FileInfo(f);
                if (finf.LastWriteTime > DateTime.Now.AddHours(-24))
                {
                    NewList.Rows.Add(f);
                }
            }
            Dts.Variables["User::FileNameArray"].Value = NewList;

            Dts.TaskResult = (int)ScriptResults.Success;
        }