Link to home
Start Free TrialLog in
Avatar of SQLSearcher
SQLSearcher

asked on

SSIS Script Task pick up the latest CSV file

Hello Experts Exchange
I have found a web page that tells me how to pick up the latest file from a folder using SSIS Scripted Task.

Here is the web page;
http://www.techbrothersit.com/2013/12/ssis-how-to-get-most-recent-file-from.html

It has a bit of code that gets the latest file name.

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

Open in new window



How do I change the code so it only looks for *.CSV files as my folder has a mixture of file types?

Regards

SQLSearcher
ASKER CERTIFIED SOLUTION
Avatar of Phil Davidson
Phil Davidson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SQLSearcher
SQLSearcher

ASKER

Hello Phil
This line of code you have given me works.

FileInfo[] files = directory.GetFiles("*.csv");

Open in new window


but the other line you have given me does not work.

FileInfo[] files = directory.GetFiles().Where(s => s.EndsWith(".csv") );

Open in new window


It has a problem with the Where and underlines it in red.

I need this line to work as I'm not sure that csv will never be in the file name.

Regards

SQLSearcher
Don't use this line:  

FileInfo[] files = directory.GetFiles().Where(s => s.EndsWith(".csv") );

Open in new window


Only use the other one.

What version of .NET are you using?

"I need this line to work as I'm not sure that csv will never be in the file name."  

Can you rephrase this comment?
Thank you very much.

I did a test with a file name that had csv in the name not the extension and it worked correctly and was not pick up by the code.

The code that worked was;
FileInfo[] files = directory.GetFiles("*.csv");

Regards

SQLSearcher