Trim space in batch script - SSIS script task

  • Hi All,
    Please help me trimming space at the end of the line in the file.

    In SSIS package I have a script task which prepares batch code to append or add footer record in the .txt file and call this batch script in execute process task. So that It adds footer record in the file.
    But, the problem is at the end of the footer record there is a space. I want to remove the space at the end.

    Example footer record : myfile.txt | 2 , After 2 i want to remove space (Where 2 is the record count in the file).

    Here is the C# 2010 code
    --------------------------------------
    string fullpath = "D:\" + "MyFile.txt";
        
        Dts.Variables["User::ConsumerBatchFile"].Value = fullpath + ".bat";

        Dts.Variables["User::ConsumerCount"].Value = (Int32)Dts.Variables["User::ConsumerCount"].Value + 2;

                          
        if (File.Exists(Dts.Variables["User::ConsumerBatchFile"].Value.ToString()))
        {
          File.Delete(Dts.Variables["User::ConsumerBatchFile"].Value.ToString());
        }

        using (StreamWriter sw = File.CreateText(Dts.Variables["User::ConsumerBatchFile"].Value.ToString()))
        {
          sw.WriteLine(@"echo " + Dts.Variables["User::ConsumerFileName"].Value.ToString() + "^|" + Dts.Variables["User::ConsumerCount"].Value.ToString() +
              " >> " + fullpath);
          MessageBox.Show(@"echo " + Dts.Variables["User::ConsumerFileName"].Value.ToString() + "^|" + Dts.Variables["User::ConsumerCount"].Value.ToString() +
              " >> " + fullpath);
          sw.Close();
          sw.Dispose();
        }

    Thnx in advnc.

  • I have managed to get it work in an alternate way. Now it is adding footer record properly with out space.
    Here is the alternate solution, this may be helpful for other.

    using (StreamWriter sw = File.AppendText(fullpath.ToString()))
        {
          sw.WriteLine(FileName + "|" + Dts.Variables["User::ConsumerCount"].Value.ToString());
        }

    Thnx..

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply