James Khan
James Khan

Reputation: 841

Importing directory of file using c#, need a filename column?

I have the following c# code for bringin in a a directory of text files and need to add a column to the output of the script component so I can capture each filename in this loop and add it to each row in a column called FILENAME. This is part of script transofrmation component in SSIS so normal derived column task wont work .

How can I do this as I cant seem to create a column and just populate it with the contents of var file ?

public override void CreateNewOutputRows()
{
    foreach (var file in Directory.GetFiles(@"C:\Pre\DataSource2_W\TextFiles\Batch1\", "*.txt"))
    { 

        string _nextLine;
        string[] _columns;
        char[] delimiters;

        delimiters = "|".ToCharArray();
        _nextLine = _reader.ReadLine();

        string[] lines = File.ReadAllLines(file, Encoding.UTF8);

        //Start at index 2 - and keep looping until index Length - 2 
        for (int i = 3; i < lines.Length - 2; i++)                  
        { _columns = lines[i].Split('|');    

                // Check if number of cols is 6
            if (_columns.Length > 4)
            {
                JazzORBuffer.AddRow();
                JazzORBuffer.Server = _columns[0];
                JazzORBuffer.Country = _columns[1];
                JazzORBuffer.QuoteNumber = _columns[2];
                JazzORBuffer.DocumentName = _columns[3];
                JazzORBuffer.CompanyNameSoldTo = _columns[4];

            }
            else
            {
                // Debug or messagebox the line that fails

                Thread t = new Thread(() => MessageBox.Show("file" + "Cols:" + _columns.Length.ToString() + " Line: " + lines[i])); 
                t.SetApartmentState(ApartmentState.STA); 
                t.Start(); 

                //MessageBox.Show("file" + "Cols:" + _columns.Length.ToString() + " Line: " + lines[i]);
                //return;
            }   
        }
    } 
}

EDIT: Updated code layout

Upvotes: 0

Views: 238

Answers (1)

Jeroen Bolle
Jeroen Bolle

Reputation: 1836

You will have to add a column to your output in Inputs and Outputs pane of the script transformation editor.

This will make the column accessible by your script.

Upvotes: 1

Related Questions