blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

SSIS Junkie

Conchango are busy and need talented consultants in and around London. Interested? Email me or send me a message

SSIS: Expressions on ForEach enumerators

I've noticed a few people on the SSIS forum lately asking about expressions on the Foreach Loop. They are aware that its possible to set expressions on properties of the Foreach container but are flummoxed when it comes to setting expressions on the properties of the enumerators themselves. That's understandable because the properties of the chosen enumerator are not available from the "Property Expressions Editor" on the "Expressions" tab of the "Foreach Loop Editor"  (which is where they usually appear). You can see that here:

 

All is not lost however. There's a seperate route into expressions on enumerator properties via the "Collection" tab of the "Foreach Loop Editor". I've highlighted that on the next screenshot that shows properties being set on the Foreach File Enumerator.

Note the "Expressions" section on the "Collection" tab. That's the bit you're interested in.

 

Dead easy when you know where to find it! I must admit it took me a while until I discovered it.

 

-Jamie

 

Published 13 October 2006 22:35 by jamie.thomson

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Danny Crowell said:

Can we set the directory at runtime from a user variable? I tried this using the Directory property in the expression editor of the foreach loop. It is not working.

The directory gets set to whatever the variable is at design time rather than runtime. How can I change the directory value to be set at runtime?

June 21, 2007 16:32
 

Danny Crowell said:

I gave up on using the Foreach loop container. I recreated the login using System.IO in a script task. I find myself using script more and more with SSIS.

June 23, 2007 04:51
 

Nick Jones said:

Hi Jamie,

I'm trying to set the "Directory" and "FileSpec" properties on a ForEach Loop programmatically but it doesn't seem to be working. I am probably missing something fundamental.

Microsoft.SqlServer.Dts.Runtime.Application a;

ForEachEnumeratorInfo forEachEnumeratorInfo = a.ForEachEnumeratorInfos["Foreach File Enumerator"];

ForEachEnumeratorHost forEachEnumeratorHost = forEachEnumeratorInfo.CreateNew();

DtsProperties hostProps = forEachEnumeratorHost.Properties;

hostProps["Directory"].SetValue(forEachEnumeratorHost, "c:\\somefolder");

hostProps["FileSpec"].SetValue(forEachEnumeratorHost, "*.txt");

I've checked the forums but can't find anything. Any help would be appreciated.

Cheers

Nick

July 16, 2007 12:50
 

jonesynick said:

Hi Jamie,

Worked it out - I was nearly there. Thought I'd post the corrected code snippet just in case anyone else is struggling in this area.

First problem was that I wasn't setting the Properties to an expression. Secondly, I needed to set the ForEachLoop.ForEachEnumerator object to the forEachEnumeratorHost object after I'd updated the properties.

ForEachLoop forEachLoop = mPkg.Executables.Add("STOCK:FOREACHLOOP") as ForEachLoop;

forEachLoop.Properties["Name"].SetValue(forEachLoop,"Loop through files");

ForEachVariableMapping forEachVariableMapping = forEachLoop.VariableMappings.Add();

forEachVariableMapping.VariableName = "User::CurrentFileName";

forEachVariableMapping.ValueIndex = 0;

ForEachEnumeratorInfo forEachEnumeratorInfo = a.ForEachEnumeratorInfos["Foreach File Enumerator"];

ForEachEnumeratorHost forEachEnumeratorHost = forEachEnumeratorInfo.CreateNew();

forEachEnumeratorHost.Properties["CollectionEnumerator"].SetExpression(forEachEnumeratorHost, "False");

forEachEnumeratorHost.Properties["Directory"].SetExpression(forEachEnumeratorHost, "\"c:\\\\somefoldername\\\\somesubfoldername\"");

forEachEnumeratorHost.Properties["FileSpec"].SetExpression(forEachEnumeratorHost, "\"*.txt\"");

forEachEnumeratorHost.Properties["Recurse"].SetExpression(forEachEnumeratorHost, "False");

forEachLoop.ForEachEnumerator = forEachEnumeratorHost;

Cheers

Nick

July 16, 2007 15:38
 

nick (not the same one) said:

Thank you for posting this - you would not believe the amount of time I wasted today trying to change the Directory property through code in a script task (visual basic).  If you have any idea how to do so, by the way, I would be grateful to hear it, but in the meanwhile, thank you thank you thank you.  a thousand nights of peace on the heads of you and yours.

December 14, 2007 00:56
 

jamie.thomson said:

Nick,

Glad to hear it was useful :)

Its not possible to do it using a script task by the way.

-Jamie

December 14, 2007 09:01
 

fsdf said:

fdsf

February 20, 2008 17:19
 

Preet said:

I am tring to use ForEach Variable Enumerator in For Each Container, I get the folder path from table and save it in variables before entering in For Each Loop container, then in For Each Variable  Enumerator how do I set so that it loops through all files of that folder?

I have added Data Flow Task under loop and in that Data Flow Task I am using Excel source by providing path to exisiting file and then using expressions to use variable name as file name. It's giving errors as "can not detach from one or more processes, object invoked has disconnected from its clients, do you want to terminate them instead"

Could someone please help me out?

Thanks in advance,

-Preet

February 20, 2008 17:25
 

imomin said:

I am new to SSIS and I was wondering if it is possible to download bunch of *.txt or *.csv file from FTP and ForEach file order by datetime created import the data into MSSQL db.

I know its possible but don't know. Also it would be nice to have video blog demonstrating some of these cool discoveries.

May 2, 2008 05:14
 

jamie.thomson said:

imomin,

The existing enumerators don't allow you to enumerate over a list of files on an FTP server although that would be a GREAT idea. Why don't you submit a request for it at http://connect.microsoft.com/sqlserver/feedback/?

-Jamie

May 2, 2008 10:24
 

Adnan said:

What about specifying the traversal order of files in a directory using the foreach loop task? Is that possible? For example, I would like to traverse all .csv files in a given folder according to the date on which they were created, ascendingly. Thanks in advance.

May 6, 2008 12:31
 

jamie.thomson said:

Adnan,

Its not possible to set the order as far as I know. You get the list back in whatever order the file system gives it to you.

What you COULD do is populate an object variable with the list of files in the order that you want and then iterate over that. Instructions are provided, in part, here: http://blogs.conchango.com/jamiethomson/archive/2005/07/04/SSIS-Nugget_3A00_-Execute-SQL-Task-into-an-object-variable-_2D00_-Shred-it-with-a-Foreach-loop.aspx and there are links to similar posts elsewhere.

-Jamie

May 7, 2008 10:15
 

Rama said:

I need to confirgure my for each loop using a variable whihc has some comma seperated values. I want to loop through each of those values in that variable. Can anyone please guide me how to go for it.

Many Thanks!!!

June 25, 2008 13:42
 

jamie.thomson said:

June 25, 2008 14:16

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication

News

Powered by Community Server (Personal Edition), by Telligent Systems