Click here to Skip to main content
15,881,248 members
Articles / Database Development / SQL Server / SQL Server 2008
Tip/Trick

SSIS: Archive or Copy Files using dynamically specifying FileSpec file types for File Enumerator in Foreach loop

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
24 Jan 2013CPOL2 min read 43.4K   5   2
How do I set FileSpec dynamically for looping files in a foreach loop container?

Introduction

How do I set FileSpec dynamically for looping files in a foreach loop container?  

We need to read or archive different FileSpec or Extention Types of files using SSIS Package and send notification after successfully archived. 

Many people may have the question on how to dynamically set the FileSpec / File Type for Foreach File Enumerator in a Foreach Loop Editor. Surprisingly I do not find a blog/article talking about this issue clearly from internet search. So I decided to compose one. 

Background

For example, if file names starting with EOD*.xls and INTRADAY*.csv are in one folder and there are other files as well you want to archive only those files that we have mentioned and send notification to team once it's copied for them. 

Using the code

It is quite easy to implement the feature if you know the property FileSpec of the Foreach File Enumerator. Now let us look at the steps: 

  1. Create five SSIS variables from the SSIS menu, as mentioned in list below:
  2. Image 1

  3. Drag a Script Task into the Control Flow panel, rename it to "Task To get filename which is going to be import", double click the task, and specify "User::gCollectionSearchFiles" into the ReadWriteVariables field.
  4. Click Edit Script... and add the following code:
  5. C#
    public void Main()
    {
        try
        {
            Variables varCollection = null;
            string header = string.Empty;
            string message = string.Empty;
    
            Dts.VariableDispenser.LockForWrite("User::gSearchFileNames");
            Dts.VariableDispenser.LockForWrite("User::gCollectionSearchFiles");
            Dts.VariableDispenser.GetVariables(ref varCollection);
    
            string[] strFileTypes = varCollection["User::gSearchFileNames"].Value.ToString().Split(',');
    
            varCollection["User::gCollectionSearchFiles"].Value = strFileTypes;
            Dts.TaskResult = (int)ScriptResults.Success;
        }
        catch (Exception ex)
        {
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
        // TODO: Add your code here
    }
  6. Drag a Foreach Loop Container task to the Control Flow panel, double click it to pop up the property window.
  7. Image 2

    Image 3

  8. Drag one more Foreach Loop inside File Type collection Foreach loop, double click inner for loop to pop up the property window.
  9. Image 4

    Image 5

  10. Drag a File system Task to archive file inside Inner Foreach Loop and double click it to pop up the property window.
  11. Image 6

  12. Last task is to send email notification, drag Send Mail Task, and set From, To , Subject and  MessageSource property to send.

Here is what the package looks like.. 

Image 7

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIncomplete Pin
Member 112954345-Feb-16 8:17
Member 112954345-Feb-16 8:17 
GeneralMy vote of 5 Pin
BihaniBrijesh31-Dec-12 9:13
BihaniBrijesh31-Dec-12 9:13 
Nice artical

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.