Reputation: 71
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.
Upvotes: 0
Views: 354
Reputation: 71
Thanks @billinKC & @KeithL
KeithL you were correct in that the \n was stripped off. So I used a slightly amended version of @billinKC's code to get what I wanted :-
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 + "~20221214\n";
System.IO.File.AppendAllText(fixedFile, str);
}
As an aside KeithL - thanks for the DateTime code however the text that I am appending is obtained from a header row in the source file which is being read into a variable in an earlier step.
Upvotes: 1
Reputation: 5594
Read all lines is likely getting rid of the \n in each record. So your replace won't work.
Simply append your string and use @billinKC's solution otherwise.
BONUS:
I think DateTime.Now.ToString("yyyyMMdd");
is what you are trying to append to each line
Upvotes: 1
Reputation: 61211
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.
Upvotes: 0