Find Last Created File in Special Directory – SSIS

There are lots of time which you want to get latest created file in a directory and do something with it, for example do a data transfer or do a file system task like delete or move.

As you may know the Foreach Loop Container in SSIS can loops through files of a directory, But it can not find latest file by creation date property.


So you need Scripting to achieve this. Fortunately SSIS have a powerful Script Task which give you Full power of .NET languages and you can do many things there.

this is the Script which returns Latest File full path in a User::LastFile variable ( code is in C# ):

 public void Main()
        {
            string[] files = System.IO.Directory.GetFiles(@"C:\SSIS\Files");
            System.IO.FileInfo finf;
            DateTime lastDate = new DateTime();
            string lastFile = string.Empty;
            foreach (string f in files)
            {
                finf = new System.IO.FileInfo(f);
                if (finf.CreationTime > lastDate)
                {
                    lastDate = finf.CreationTime;
                    lastFile = f;
                }
            }

            Dts.Variables["User::LastFile"].Value = lastFile;

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

As you see that was simple few lines of code that get result as you want. I hope SSIS Developers be familiar with .NET languages and use the power of Script Task each time they need.

There is another type of problems which Users need to find files in a directory which modified in few hours ago.

For example you want to loop through files of directory which modified in 24 hours ago, You can do this with this script :

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;
        }

Note that FileNameArray is an Object type variable, and after filling it in this script task, you can loop through it with Foreach loop container which enumerator configured as ado enumerator.

Hope these helps.

Reza Rad on FacebookReza Rad on LinkedinReza Rad on TwitterReza Rad on Youtube
Reza Rad
Trainer, Consultant, Mentor
Reza Rad is a Microsoft Regional Director, an Author, Trainer, Speaker and Consultant. He has a BSc in Computer engineering; he has more than 20 years’ experience in data analysis, BI, databases, programming, and development mostly on Microsoft technologies. He is a Microsoft Data Platform MVP for 12 continuous years (from 2011 till now) for his dedication in Microsoft BI. Reza is an active blogger and co-founder of RADACAD. Reza is also co-founder and co-organizer of Difinity conference in New Zealand, Power BI Summit, and Data Insight Summit.
Reza is author of more than 14 books on Microsoft Business Intelligence, most of these books are published under Power BI category. Among these are books such as Power BI DAX Simplified, Pro Power BI Architecture, Power BI from Rookie to Rock Star, Power Query books series, Row-Level Security in Power BI and etc.
He is an International Speaker in Microsoft Ignite, Microsoft Business Applications Summit, Data Insight Summit, PASS Summit, SQL Saturday and SQL user groups. And He is a Microsoft Certified Trainer.
Reza’s passion is to help you find the best data solution, he is Data enthusiast.
His articles on different aspects of technologies, especially on MS BI, can be found on his blog: https://radacad.com/blog.

Leave a Reply