MS-DOS is alive – within the SSIS ForEachLoop-Container!
Stumbling upon the legacy of a 20 year old operating system is odd enough, but stumbling in Integration Services is even more fun!
But first things first: if you need to process multiple files you will sooner or later put the ForEach-Loop-Container to use. Using the ForEachLoop, you can iterate over files with a certain name structure within a folder quite easily. A scenario for importing multiple files might involve reading multiple Excel-files within a folder. A possible configuration of the ForEach-Loop-Container is shown below:

The rest of the package then might look like this: within the loop a variable with the current file path is read in order to print out all found files in a message box after the container:

I have yet to show you the content of our folder, so here it is:

As expected, the package finds both Excel files – awesome! But wait, did we really expect the package to find the Excel 2007 file as well? After all the file has the extension .xlsx, which is actually quite helpful since both Excel file types differ significantly and might need separate processing within SSIS. So what if we wanted to exclude Excel 2007 files in the first place? Double-checking the configuration of the ForEach Loop Container reveals no helpful information – no matter what expression we insert into the “Files” field, the container cannot be modified to catch .xls files only. The expression *.xls always behaves like *.xls*!
At this point, Books Online once again prove to be helpful and reveal that the observed behavior is purely intentional – the expression behaves just like it would using the DIR-command in Windows (see http://msdn.microsoft.com/en-us/library/ms187670.aspx). Due to compatibility issues, the DIR-command is implemented in such a manner, that it “also returns files that have the same extension with additional characters appended” – the MS-DOS 8.3 file name restriction strikes again!
As a consequence of this behavior, we need to differentiate between file extensions within the ForEach-Loop-Container itself. For example, we could add a Precedence Constraint which evaluates an expression that checks whether the last three letters of the current file name are really x, l and s:


Using this modification, the package runs as expected and only finds a single Excel file, ignoring the Excel 2007 file:

So, is it a bug or a feature?! Once again SSIS has been proven to be at times, but in the end that is what makes it well worth loving – you always discover something new along the way!
English
Deutsch

ixto-Blog RSS Feed














