SSIS – How to loop through files in folder and get path+file names and finally execute stored Procedure with parameter as Path + Filename

Looks like you have the right idea in general and the link @Speedbirt186 provided has some good details but it sounds like there are a couple of nuances that I thought I might point out in regards to flow and variables.

The foreach loop can assign the entire path or the file name or file name & extension to a variable. The latter will be the most help in your case if you don’t want to add a script task to split the Filename from the path. If you start by adding 5 variables to your project it will make it a little easier. 1 will be the Source Directory Path, another the Destination (Archive) Directory Path, and then 1 to hold the File Name and Extension assigned by the for each loop. Then 2 additional dynamic variables that simply combine the source directory and file name to get the source full path and the destination with file name to get the destination full path.

enter image description here

Next make sure you set up your database and Excel file connections. In your Excel file connection after setting it up go to Expressions in the properties window and set the “Connection String” property to SourceFullPath. This will tell the connection to change the file path at every iteration of your loop.

enter image description here

Now you just need to setup your loop etc. Add the fore each loop container setting a directory, filter, and choose File Name and Extension.

enter image description here

Now in the expression box on the collection page set the directory property to be that of your Source Directory variable.

enter image description here

The last part of the Fore each loop is to set your variable mappings to store the file name in your variable. so go to that tab choose your file name variable and set index to 0.

enter image description here

At this point you can add your data flow and setup your import just like you would with a normal file (note your default value for your file name parameter should be to an actual file with the structure you will want to import).

After your data flow drop in your Execute SQL task and set it up how you need. here is an example of direct input and you can see an easy way to reference a parameter is simply a question mark (?).

enter image description here

Next in your sql task setup your parameter mapping by adding in the details you need such as:

enter image description here

Now you are on to your file task. Drop your file task and setup as you desire, but choose your destination and source full path variables to tell the task which file to move.

enter image description here

that’s it your are done. there is 1 more thing to note though. The way you have your precedence set in the image you posted you show going from your data flow to your sql and to your file task simultaneously. If your stored procedure relies on your file you may want to put it after your sql task. You can always change the constraint options to “completion” if you want to move the file even if your stored proc fails.

enter image description here
enter image description here

Leave a Comment