Click here to Skip to main content
15,886,093 members
Please Sign up or sign in to vote.
3.50/5 (5 votes)
See more:
To say I'm new at this would be understating.
My point of pain?
I have a file, lets call it "1.csv".
I have another file, let's call it "2.csv".
what I am trying to do is append "2.csv" with "1.csv" without overwriting the contents of 2.csv.
I don't need to worry about format or column headers as they are the same.

"1.csv" is new data and "2.csv" would be a "master" file.
For instance, if "1.csv" has 10 rows and "2.csv" has 10 rows, when the files are combined the new file named "2.csv" would have 20 rows.

I hope this makes sense. I am trying to automate as much as possible to make my day easier and programming is a frustrating blast, but when it works, what a RUSH!

Thanks for any help!
Nick
Posted
Updated 13-Jul-21 8:12am

The simplest way is probably
C#
using System.IO;
...
File.AppendAllText(originalFilePath, File.ReadAllText(appendFilePath));


[EDIT]
Reading your comments, I wonder what went wrong that it does not work with you. The above appends one text file to an existing one. If the CSV file is not too elaborate (i.e. no multi-line fields, terminates properly on a new-line), then there should not be any format issues (i.e. that all is now in one column(?) as you report). The only thing I could imagine is the character encoding. That's why it was good to see your files (or at least a small and obfuscated portion of it).
[/EDIT]

Cheers
Andi
 
Share this answer
 
v2
Hello!

Try something like this...

C#
StreamReader rdr = new StreamReader("<pathtoyourmasterfile>");
string master = rdr.ReadToEnd();
rdr.Close();

rdr = new StreamReader("<pathtoyournewdatafile>");
string newdata = rdr.ReadToEnd();
rdr.Close();
rdr.Dispose();

//The New Data .csv file will have headers. Need to remove those.
newdata = newdata.Substring(newdata.IndexOf('\n') + 1);

StreamWriter wtr = new StreamWriter("<pathtoyourmasterfile>");
wtr.Write(master + "\r\n" /*That \r\n may or may not be necessary*/ + newdata);
wtr.Close();
wtr.Dispose();


I hope you understand how it works! If you want clarification let me know :-)
 
Share this answer
 
v4
Comments
Member 10654601 9-Mar-14 9:07am    
Hey Careful Coder!
I used your suggestion and it seems to be working perfectly!
StreamReader rdr = new StreamReader("<pathtoyourmasterfile>");
string master = rdr.ReadToEnd();
rdr.Close();

rdr = new StreamReader("<pathtoyournewdatafile>");
string newdata = rdr.ReadToEnd();
rdr.Close();
rdr.Dispose();

//The New Data .csv file will have headers. Need to remove those.
newdata = newdata.Substring(newdata.IndexOf('\n') + 1);

StreamWriter wtr = new StreamWriter("<pathtoyourmasterfile>");
wtr.Write(master + "\r\n" /*That \r\n may or may not be necessary*/ + newdata);
wtr.Close();
wtr.Dispose();

Thank you!

How fun to have this happen automatically! I hope this continues to be fun as I adjust and finish building this project out.

This is a great resource and I appreciate everyones input.

Nick
Member 10654601 17-Mar-14 13:37pm    
Hey All!

Just circling back to let you know I have my little program working great!
Thanks for all the help!
public static void AppendCsvFiles(string baseFile, string[] newFiles, string outFile)
{
    var baseLines = File.ReadAllLines(baseFile).ToList();
    Log.Info($"Total Lines in Base File: {baseLines.Count}");
    foreach (var f in newFiles)
    {
        var newLines = File.ReadAllLines(f);
        baseLines.AddRange(newLines.Skip(1));
        Log.Info($"Total Lines in {f}: {newLines.Length}");
    }
    Log.Info($"Total Lines in Combined File: {baseLines.Count}");
    File.WriteAllLines(outFile, baseLines);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900