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
Visual C++.NET.NET ProgrammingMicrosoft SQL ServerSSIS

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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
Avatar of Phil Davidson
Phil Davidson
Flag of United States of America image

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?
Avatar of SQLSearcher
SQLSearcher

ASKER

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
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo