2

I'm working on an SSIS package that will be used to import data from an Excel file into SQL Server. My current struggle is figuring out how to make the SSIS package bring in exactly one excel file without knowing the name of it beforehand. I have a directory that will contain between 0 and n excel files at the same time. I want to pull in only the file with the oldest creation time. Is this possible?

I'm using Visual Studio 2015 to build the SSIS package. My DB is in SQL Server 2016.

1 Answer 1

1

To create a dynamic file connection:

  1. Create a new Variable (Name Example: 'SourceFile') of datatype String.

  2. In a 'For Each Loop Container' map that variable under the 'Variable Mapping' Tab and set the 'Enumerator Configuration' to the correct folder and file extension. The 'For Each Loop Container' will read the file from the location and assign the name of the file to the variable.

  3. In the Expressions Properties of your file connection set the ConnectionString property to @[User::SourceFile]

This should make your file source dynamic. It will pick up the file no matter what it is named, but the format of the file will have to be consistent.

Using just SSIS tasks, I am not aware of how to utilize the create date of the files to pick the oldest file, but if the file name contains the create date of the file you could substring the date out of @[User::SourceFile] variable and store it in another variable with each execution of the 'For Each Loop Container' to determine which file is oldest.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. The problem that I'm having is that I only want to bring in a single file at a time, not all of the files in the folder. If I do this, won't the package bring in all files in that location?
If no other tasks are inside the For Each Loop it will only read the file name and assign it to @[User::SourceFile]. It won't move any files or do any other tasks unless you put those tasks in the loop. You could use the loop to only determine the name of the oldest file and once you know the name of the oldest file, assign that value to another variable (example: @[User::SourceFileMaxCreateDate]) and use that new variable in the ConnectionString property of the file connection
Huh. That did not occur to me. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.