buzzzzjay
buzzzzjay

Reputation: 1150

Creating an Expression for an Object Variable?

I am creating an SSIS package and the first step is to get a list of files stored in a file and store this information in an object variable. I pass this variable to a task that tries to open the list of files. However, the open file task fails if there are no files to open because the folder is empty.

I am relatively new to using SSIS and was curious on how to rewrite an expression that checks to see if the object variable is empty and if it is empty not allow the open file task to run.

Upvotes: 0

Views: 1153

Answers (1)

user756519
user756519

Reputation:

You don't need to use an object variable to loop through files in a folder. You can easily achieve that using Foreach Loop Container. Following example demonstrate how this can be done. The example was created in SSIS 2008 R2.

Step-by-step process:

  1. Let's assume that we need to loop through the files in the path C:\temp as shown in screenshot #1.

  2. On the SSIS package, create three variables namely FolderPath, FilePattern and FilePath. Refer screenshot #2. Set the FolderPath variable to the folder that you would like to loop through, here in this case I have chosen C:\temp. Set the FilePattern variable to a pattern that your files should match, here I would like to loop through all files so I have used *.*. If you would like to loop through only Excel 2010 files, then you can use *.xlsx. It can accept only one pattern. Don't set any value to variable FilePath because this will be assigned with a value when the Foreach Loop Container loops through each file in the folder.

  3. On the package's Control Flow Tab, place a Foreach Loop container and then place a Script task within the Foreach loop container. In this example, we are going to simply loop through each file and display their names and not do anything more. Refer screenshot #3.

  4. Configure the Foreach Loop Container as shown in screenshots #4 and #5. On the Collection section, we have configured the Expressions to use the variables FolderPath and FilePattern. On the Variable Mappings section, we have told the container to store the file path value into the FilePath variable.

  5. Inside the Script task, replace the Main() method code with the code given under Script Task Code section. There is nothing fancy in the code. It simply displays the file path in a message box.

  6. Screenshots #6 - #11 shows a sample package execution and how each file in the folder is looped through. Please note the screenshot #11, the script task is marked with green color stating that the package successfully executed the task.

  7. Now, let's delete all the files in the folder C:\temp as shown in screenshot #12.

  8. If we execute the package now, the tasks within the Foreach loop container will not executed because there are no files to loop through and the folder is empty. Refer screenshot #13. Please note that the script task is marked with white color stating that the package didn't execute the script task and it skipped the section.

  9. This is only a simple example. You can do whole lot more than simply display names of the files. You can have other tasks within the Foreach Loop container and pass the FilePath variable to process the files.

Hope that helps.

Script task code:

C# code that can be used only in SSIS 2008 and above.

public void Main()
{
    Variables varCollection = null;

    Dts.VariableDispenser.LockForWrite("User::FilePath");
    Dts.VariableDispenser.GetVariables(ref varCollection);

    MessageBox.Show(varCollection["User::FilePath"].Value.ToString(), "File Path");

    Dts.TaskResult = (int)ScriptResults.Success;
}

Screenshot #1:

1

Screenshot #2:

2

Screenshot #3:

3

Screenshot #4:

4

Screenshot #5:

5

Screenshot #6:

6

Screenshot #7:

7

Screenshot #8:

8

Screenshot #9:

9

Screenshot #10:

10

Screenshot #11:

11

Screenshot #12:

12

Screenshot #13:

13

Upvotes: 4

Related Questions