1

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

MZEN
  • 155
  • 1
  • 6

1 Answers1

2

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:

Files:
BaseFileName*

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

Files:
BaseFileName*.xlsx

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.

Edit:

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.

Eric Brandt
  • 7,886
  • 3
  • 18
  • 35
  • Thank you. 2 clarifications: Is the filepath have to point to a specific file, even though it will be looping through multiple? When I list a folder there, it doesn't work. Also, when you say use the variable as a input parameter, is that in the expressions area under connectionstring? and then variable as the expression? – MZEN Aug 30 '19 at 19:45
  • Validation error. Package1 Connection manager "Excel Connection Manager": The connection string format is not valid. It must consist of one or more components of the form X=Y, separated by semicolons. This error occurs when a connection string with zero components is set on database connection manager. – MZEN Aug 30 '19 at 20:00
  • 1
    Edited the answer as best I could. :) – Eric Brandt Aug 30 '19 at 20:08