Search code examples
c#ssisssis-2012script-task

How to reset object variable from script task in SSIS?


I am quiet a bit new in SSIS package writing and I have nested loop in my SSIS Package, one is looping for all the folders into a location and inner loop is looping through all the files in each folder.

In folder level loop my first task is script task in which I am fetching all the files path into an object variable with package level scope as a array of string as below.

string targetDirectory = Convert.ToString(Dts.Variables["SourceFolderLocation"].Value);
string[] fileEntries = Directory.GetFiles(targetDirectory, "*.pdf");
Dts.Variables["FileList"].Value = fileEntries;

Dts.Variables["FileList"].Value is my object variable with package level scope.

Now my requirement is in script task of folder level loop, first to reset that object variable and then set new files list into that object variable as if I get any exception to access folder it should not process previous folder's files.

My question is how to reset object variable in script task c# code? So it do not process previous folder's files again and also I do not get enumerator should not contain null value error.

Any help will be greatly appreciated.


Solution

  • If I'm understanding correctly, you should be able to set it to a new string[] before that call to GetFiles to 'reset' it.

    string[] fileEntries = new string[] {};
    try {
        string targetDirectory = Convert.ToString(Dts.Variables["SourceFolderLocation"].Value);
        fileEntries = Directory.GetFiles(targetDirectory, "*.pdf");
        Dts.Variables["FileList"].Value = fileEntries;
    }
    catch (Exeception e) { 
        // handle exception
    }