3

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.

Naim Halai
  • 355
  • 1
  • 8
  • 27

1 Answers1

1

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
}
Hadi
  • 36,233
  • 13
  • 65
  • 124
sorrell
  • 1,801
  • 1
  • 16
  • 27
  • It might be the old "C" programmer I started out as, but won't this solution leave the FileList variable's former contents in memory when you assign it to a new string array? Or will garbage collection handle this? – samp Jan 22 '19 at 17:55
  • Good eye - check the 'Memory Disposal' section here (http://www.albahari.com/valuevsreftypes.aspx). I think it will be collected and disposed of by the GC. – sorrell Jan 22 '19 at 18:11