Home » excel » excel – SSIS won't execute foreach loop for dynamic xlsx filename

excel – SSIS won't execute foreach loop for dynamic xlsx filename

Posted by: admin May 14, 2020 Leave a comment


I have a xlsx file that will be dropped into a folder on a monthly basis. The filename will change every month (filename_8292019) based on the date, to which I cannot change.

I want to build a foreach loop to pick up the xlsx file and manipulate it (load into SQL server table, the move the file to an archive folder). I cannot figure out how to do this with a dynamic filename (where the date changes.

I was able to successfully run the package when converting the xlsx to CSV, and also when pointing directly to the xlsx filename.

[Flat File Destination [219]] Error: Cannot open the datafile “filename”
OR errors relating to file not found

How to&Answers:

The Files: entry on the Collection tab of the Foreach Loop container will accept wildcard characters.

The general pattern here is to create a variable, say, FileName. Set your Files: to something like:


or, if you want to be sure to only pick up spreadsheets, maybe:


Select either Name and extension or Fully qualified, which will include the full file path. I usually just use Name and extension and put the file path into another variable so when Ops tells me they’re moving my drop location, I can change a parameter instead of editing the package. This step tells the container to remember the name of the file it just found so you can use it later for a variable mapping.

On the Variable Mappings tab, select your variable name and assign it to Index 0.

Then, for each spreadsheet, the container will loop, pick up the name of the first file it finds that matches your pattern, and assign the full name, with the date extension (and path, if you go that way), to your variable. Pass the variable as in input parameter to the tasks inside the loop and use that to process the file, including moving it to the archive, or you’ll get yourself into an infinite loop, processing the same file(s) over and over. <–Does that sound like the voice of experience? Yeah. Been there, done that.


Here, the FullFilePath variable is just the folder name, without a file reference. (Red variable to red entry in the Folder box).

The FileBaseName variable drives what shows up in the Files box. (Blue to blue).

Another variable picks up the actual file name, with the date extension. Later, say in a File System Task, if I need the folder & file name together, I concatenate the variables.

enter image description here

As far as the Excel Connection Manager error you’re getting, unfortunately I’m no help. I don’t use it. We have SentryOne’s Task Factory for SSIS which includes a much more resilient Excel connector.