Loop through folder to get lates file

  • Hi,

    my third post today!

    i wan to be able to look through a folder and pick up (and then load) the latest file. For now, lets say the date format of the file will be ddmmyyyy

    i have seen various articles about using the foreach loop container..creating the filename as a variable and so forth but since i am a newbie, i am stuck on how to start this. Any advice or a handy tutorial which might help?

    thanks

    haythem

  • i should add i have now created a dynamic connection string...but just need to apply a loop criteria to check for the file.

  • i've used an expression to do this, but had the benefit of a file name in the format of filenameYYYYMMDD. in the for each loop assign the file name to varFullFileName, then use a ternary expression like below to assign the latest file to varFullFileNameLatest:

    @[User::varFullFileName] > @[User::varFullFileNameLatest] ? @[User::varFullFileName] : @[User::varFullFileNameLatest]

    [font="Arial Narrow"]bc[/font]

  • You can use a Script Task to retrieve the file creation date. This may get you started:

    Imports System

    Imports System.Data

    Imports System.IO

    Imports System.Math

    Imports Microsoft.SqlServer.Dts.Runtime

    Public Class ScriptMain

    Public Sub Main()

    Dim FileDate As DateTime = File.GetCreationTime("C:\AndyOut.txt")

    MsgBox(FileDate)

    Dts.TaskResult = Dts.Results.Success

    End Sub

    End Class

    :{> Andy

    Andy Leonard, Chief Data Engineer, Enterprise Data & Analytics

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply