I am creating an SSIS package where I have to iterate through some of the selected folder and I want to store the folder names in an array to keep track of folders that I have processed. Can I keep an array in SSIS package and keep appending the value in that array?
-
1Yes, but perhaps you could explain better what you're doing. There are native tools for folder enumeration and keeping track of what's been visited so I'm worried you're reinventing the wheel.billinkc– billinkc2014-01-04 23:54:13 +00:00Commented Jan 4, 2014 at 23:54
-
Thanks a lot for your answer. So basically I am trying to do these steps: 1. getting files and moving them to corresponding date folder according to file name 2. Since there are many existing previous date folders , so i need to keep track of the date folder where i move the file to for further processing. 3. Now I need to run a tool only in those folders where i moved the file. My initial assumption was to keep a SSIS object and add the date folder name when iterating through the file in for loop but I am not able to add multiple string in a SSIS package as an array.user2514908– user25149082014-01-06 14:51:05 +00:00Commented Jan 6, 2014 at 14:51
-
If you're not actually doing any data manipulation I would suggest doing this in a standalone script instead of SSISNick.Mc– Nick.Mc2014-01-07 00:37:20 +00:00Commented Jan 7, 2014 at 0:37
-
Thanks ElectricLlama for the answer. I understand your point but i am doing data manipulation lateruser2514908– user25149082014-01-07 22:25:55 +00:00Commented Jan 7, 2014 at 22:25
1 Answer
You can store the value of a for loop variable in an array. Doing this is a little messy IMO. There's likely a cleaner approach using "out of the box" SSIS functionality as @billinkc suggested. However, here are some pointers ...
Let's go with your scenario where you have a for each loop that iterates over some files (using a Foreach File Enumerator) and you want to store the folder names in an array.
Here are some variables we'll use:

FolderList will be the array and CurrentFile will be the for loop variable. The package in its simplest form might look like this:

In the script task, the code might look like this. I've chosen to use a List<string> as my array type, but you could use something else, such as ArrayList. (Note: you'll need to add using statements for System.Collections.Generic and System.IO for the code below):
public void Main()
{
//get current directory
string directory = Path.GetDirectoryName(Dts.Variables["User::CurrentFile"].Value.ToString());
List<string> lst = new List<string>();
// if the FolderList is already a List<string> then set set it to lst
if ((Dts.Variables["User::FolderList"].Value is List<string>))
{
lst = (List<string>)Dts.Variables["User::FolderList"].Value;
}
// if the directory isn't in the list yet, then add it to the list
if(!lst.Contains(directory))
{
lst.Add(directory);
}
// update our variable with the List<string>
Dts.Variables["User::FolderList"].Value = lst;
Dts.TaskResult = (int)ScriptResults.Success;
}
Each time the Script Task is executed, you'll get a new folder added to the array. Once the for each loop is done, you may want to examine the values of the array. You can do this using a Script Task (similar to what we did above):
List<string> lst = (List<string>)Dts.Variables["User::FolderList"].Value;
// do stuff with lst
You can also iterate over the values in the array using a for each loop (use the Foreach From Variable Enumerator), which I just learned as I was walking through this (thanks!). Just set the variable to enumerate over to your array variable (here FolderList) and specify another variable (e.g. CurrentFolder) as index 0 in Variable Mappings. This worked for a List<string>, but I'm not sure what other collection types it would work with.