record count in script task
I have a flat file (c:\sometextfile.txt).     Does anyone have an example of how to count the number of records in this file using a script task?
March 23rd, 2015 7:10pm

May I ask why you want to do that operation using Script task ? Instead you could use Row Count transformation after the Flat file source and capture the row count. 
Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 10:48pm

This thread will help you: 

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b325defc-e76a-42aa-ad7a-afbfa55cd9f6/record-count-from-flat-file

March 23rd, 2015 10:51pm

Hi sqlguy10,

According to your description, you want to count the number of records in a flat file using a Script Task.

Based on my test, the easiest way is to use a Data Flow Task with Flat File Source for text file and use Row Count Transformation to get value onto a variable, then use a Script Task to show the variable value. For more details, please see:

  1. Create a variable named variable with Int32 data type.
  2. Create a Flat File Connection Manager that connects to the text file.
  3. Drag a Flat File Source that connects to the Flat File Connection Manager in a Data Flow Task.
  4. Drag a Row Count Transformation connects to the Flat File Source.
  5. Drag a Script Task connects to the Data Flow Task.
  6. Double-click the Script Task to add the variable to the ReadOnlyVariables property, then add the following code:
    public void Main()
    {
          // TODO: Add your code here
          string Variables = Dts.Variables["User::Variable"].Value.ToString();
          MessageBox.Show(Variables);
    Dts.TaskResult = (int)ScriptResults.Success;
    }

The following screenshot is for your reference:

If there are any other questions, please feel free to ask.

Thanks,
Katherine Xiong

If you have any feedback on our support, please click here.

Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 11:00pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics