Vision Binary

work hard, be nice, amazing things will happen


SSIS: Get any flat file source from folder and cache the name as a super global veriable

Step-by-step process:

  1. On the SSIS package, create 3 variables are shown in screenshot #1. Scope CheckFilerepresents the package name. Variable Folder will represent the folder that you would like to check for the file. Filename represents the file name to check for. Variable FilePath will be the global variable that you will need. It will be filled in with the file path value if the file exists, otherwise it will be empty.

  2. On the package’s Control Flow tab, place a Foreach Loop container and a Script Task. Script Task is to showcase that the variable retains the value after the Foreach Loop container execution is complete. Refer screenshot #2.

  3. Configure ForEach Loop container as shown in screenshots #3 and #4.

  4. Replace the Main() method within the Script Task with the code given under the Script task code section. This is to demonstrate the value retained by the variable FilePath.

  5. Screenshots #5 shows no files exist in the path c:\temp\ and screenshot #6 shows the corresponding package execution.

  6. Screenshots #7 shows the file TestFile.txt exists in the path c:\temp\ and screenshot #8shows the corresponding package execution.

  7. If you would like to process the file when it exists, you can place a Data Flow Task within theForeach Loop container to do that.

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.LockForRead("User::FilePath");
            Dts.VariableDispenser.GetVariables(ref varCollection);

            if (String.IsNullOrEmpty(varCollection["User::FilePath"].Value.ToString()))
            {
                MessageBox.Show("File doesn't exist.");
            }
            else
            {
                MessageBox.Show("File " + varCollection["User::FilePath"].Value.ToString() + " exists.");
            }

            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

 

http://stackoverflow.com/a/6229877



Leave a comment