开发者

Dynamically made flat file connection - want to take most recent file in the folder using SSIS?

I have created one SSIS package that takes .csv file data and insert into SQL table,

But now I want most recently added file in that folder suppose

My flat file connection path is : C:\Temp

Now in temp there is my file named MYDATA 2011-09-08.csv

now some more files are there like MYDATA 2011-09-15.csv , MYDATA 2011-09-17.csv

Now MYDATA 2011-09-17.csv is the 开发者_如何转开发most recent by date and i want to pickup this file how can i do this???


I would complete this task with a foreach loop combined with a script task.

  • First you should declare 3 variables:

Dynamically made flat file connection - want to take most recent file in the folder using SSIS?

  • Next step is adding a foreach loop container to the package with a script component inside of it.

Dynamically made flat file connection - want to take most recent file in the folder using SSIS?

  • Set foreach loop container properties as follows.

Dynamically made flat file connection - want to take most recent file in the folder using SSIS?

Dynamically made flat file connection - want to take most recent file in the folder using SSIS?

  • Set variables which script component has access to

Dynamically made flat file connection - want to take most recent file in the folder using SSIS?

  • In the script component parse the date from the file name, check if it greater than the recent date (User::RecentDate), and if it is save it to the User::RecentFileName variable.

    string strDatePart = Path.GetFileNameWithoutExtension(
        Dts.Variables["User::FileName"].Value.ToString()).Substring(7);
    DateTime dateTime = DateTime.MinValue;
    if (DateTime.TryParse(strDatePart, out dateTime))
    {
        if (dateTime > (DateTime)Dts.Variables["User::RecentDate"].Value)
        {
            Dts.Variables["User::RecentDate"].Value = dateTime;
            Dts.Variables["User::RecentFileName"].Value =           
                Dts.Variables["User::FileName"].Value;
        }
    
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    else
        Dts.TaskResult = (int)ScriptResults.Failure;
    

Of course you can change this script as you wish it's just an example. After the loop ends the User::RecentFileName will contain your desired result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜