Sunday, February 19, 2012

For Each from Variable Enumerator

Hi ,

Can someone help me in using the "For each from variable enumerator".

The work around that I have made so far is

1) Generating a Enumerated Value of File collection using Script Control

2) A For each Collection container that processes a given set of files.

the above two conditions works for me when executed independently. But I am not able to relate these two, so as to give a enumerator input to the "For each from Variable Enumerator"

any help will be highly appreciated...Thanks

For each from variable enumerator can enumerate over variable that contains .NET collection, e.g. ArrayList. Using it is a bit complex, let's go step by step:

First, create a variable that will contain the collection, let's call it User::Collection, set variable type to Object.

Now populate it in Script Task: create Script Task, specify that it uses this variable for read/write, add a code like this inside Main function:

Dim arr As New ArrayList
arr.Add("D:\Tests\a.txt")
arr.Add("D:\Tests\b.txt")
Dts.Variables("User::Collection").Value = arr

Now add a ForEach loop, specify that it uses Foreach From Variable enumerator, and the variable is User::Collection.

Switch to Variable mapping page of Foreach Loop Editor. We'll need another variable to hold the current value of the enumerator, so click a cell under Variable, select a <New Variable>, create a variable called EnumValue. Leave it associated with Index 0 (our enuration value has only one value).

OK, now add something inside the loop to be executed for each iteration. I choosed File System task, to copy files from the enumerated value to directory c:\tests\destination folder. You can use other tasks, e.g. Data Flow that uses the file in a Flat File Source Adapter.

Open File System task editor. Set operation to CopyFiles. Create source connection (using any file, we'll change it anyway later), destination connection to destination folder. Close the File System task editor.

Now we need to tell the connection manager to use our enumerated value. Select the source connection manager. In Properties panel, select Expressions property and click '...'. Select property ConnectionString, click '...' and drag the EnumValue variable to expression field. The expression should be @.[User::EnumValue].

Finally, we need to set DelayValidation property of File System task to TRUE. The reason is that since the source connection manager gets the value from the variable, we should not validate the task in the beginning of the package.

OK, now run the package! The files added in the script task should get copied to destination folder.

No comments:

Post a Comment