Question

I have multiple file in a folder with a naming convention

Name_MoreName_DDMMYYYY_SomeNumber_HHMMSS.txt

How can I take only one file in a foreach loop which has oldest Date and Time (i.e. oldest DDMMYYYY and HHMMSS). I am restricted to use the foreach loop.

Ex:

  • Name_MoreName_22012012_SomeNumber_072334.txt
  • Name_MoreName_22012012_SomeNumber_072134.txt
  • Name_MoreName_24012012_SomeNumber_072339.txt
  • Name_MoreName_22012012_SomeNumber_072135.txt

So the oldest file will be

Name_MoreName_22012012_SomeNumber_072134.txt

how can i take the oldest file only ?

Was it helpful?

Solution

Date Expression:

SUBSTRING( @[User::Filename], FINDSTRING( @[User::Filename],"_" , 2) +1 , 8)

OldestDate Expression:

(DT_I4) (RIGHT( @[User::Date] , 4)+ SUBSTRING( @[User::Date] , 3,2)+ SUBSTRING( @[User::Date] ,1, 2))

Script Task - I

Public Sub Main()
        Dim OldestDate As Integer
        Dim CurrentDate As Integer
        OldestDate = CType(Dts.Variables("OldestDate").Value, Integer)
        CurrentDate = CType(Dts.Variables("CurrentDate").Value, Integer)
        If OldestDate > CurrentDate Then
            Dts.Variables("OldestFile").Value = Dts.Variables("Filename").Value.ToString()
            Dts.Variables("CurrentDate").Value = CType(Dts.Variables("OldestDate").Value, Integer)
        End If
        Dts.TaskResult = Dts.Results.Success
    End Sub

Script Task - II

Public Sub Main()
        MsgBox(Dts.Variables("OldestFile").Value.ToString())
        Dts.TaskResult = Dts.Results.Success
End Sub

enter image description here

Explanation:

The basic idea here is get the date part i.e. DDMMYYYY and convert it to YYYYMMDD. Now, we can compare which date is greater by simple integer comparision as long as the date is in YYYYMMDD format. We start with comparing the first file's date with 19000101 ( a date well in past) and then as seen in the Script task - I, we assign the OldestFile with the Filename conditionally. The Script Task - II printsout the filename with the oldest date.

OTHER TIPS

The existing Foreach (file) Enumerator would not suffice in this case as it is limited to a file name mask. For your needs, you're looking at a Script Task to populate something. Whether that something is just a variable which is then used to configure the ConnectionString property of a Flat File Connection Manager or whether you need to generate a list of all the old files (in case you need to identify Name_MoreName_OldestDate and Name_OtherName_OldestDate), assign that to a variable and then use a Foreach Enumerator (variable) will depend on your exact business needs.

If you can use the modified date/time, then this answer would be an inversion of the logic in this answer Import most recent csv file to sql server in ssis Otherwise, similar logic without the LINQ fun. Go through all the files in a folder with the DirectoryInfo call and then compare file names.

Let me know if you need a more detailed explanation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top