Search code examples
ssisflat-file

SSIS Script howto append text to end of each row in flat file?


I currently have a flat file with around 1million rows. I need to add a text string to the end of each row in the file.

I've been trying to adapt the following code but not having any success :-

public void Main()
{
    // TODO: Add your code here

    var lines =  System.IO.File.ReadAllLines(@"E:\SSISSource\Source\Source.txt");
    foreach (string item in lines)
    {
        var str = item.Replace("\n", "~20221214\n");
        var subitems = str.Split('\n');
        foreach (var subitem in subitems)
        {
            // write the data back to the file

        }
    }
    Dts.TaskResult = (int)ScriptResults.Success;
}

I can't seem to get the code to recognise the carriage return "\n" & am not sure howto write the row back to the file to replace the existing rather than add a new row. Or is the above code sending me down a rabbit hole & there is an easier method ??

Many thanks for any pointers &/or assistance.


Solution

  • I read your code as

    For each line in the file, replace the existing newline character with ~20221214 newline

    At that point, the value of str is what you need, just write that! Instead, you split based on the new line which gets you an array of values which could be fine but why do the extra operations?

    string origFile = @"E:\SSISSource\Source\Sourcetxt";
    string fixedFile = @"E:\SSISSource\Source\Source.fixed.txt";
    
    // Make a blank file
    System.IO.File.WriteAllText(fixedFile, "");
    
    var lines =  System.IO.File.ReadAllLines(@"E:\SSISSource\Source\Source.txt");
    foreach (string item in lines)
    {
        var str = item.Replace("\n", "~20221214\n");
        System.IO.File.AppendAllText(fixedFile, str);
    }
    

    Something like this ought to be what you're looking for.