I am wanting to append 6 CSVs that have identical layouts and headers together.
I’ve been able to accomplish this by loading each of the 6 csvs into their own seperate data tables and removing the first row of each datatable. Finally I’ve appended them together using the ImportRow method.
DataTable table1 = csvToDataTable(@"C:\Program Files\Normalization\Scan1.csv");
DataTable table2 = csvToDataTable(@"C:\Program Files\Normalization\Scan2.csv");
DataTable table3 = csvToDataTable(@"C:\Program Files\Normalization\Scan3.csv");
DataTable table4 = csvToDataTable(@"C:\Program Files\Normalization\Scan4.csv");
DataTable table5 = csvToDataTable(@"C:\Program Files\Normalization\Scan5.csv");
DataTable table6 = csvToDataTable(@"C:\Program Files\Normalization\Scan6.csv");
foreach (DataRow dr in table2.Rows)
{
table1.ImportRow(dr);
}
foreach (DataRow dr in table3.Rows)
{
table1.ImportRow(dr);
}
foreach (DataRow dr in table4.Rows)
{
table1.ImportRow(dr);
}
foreach (DataRow dr in table5.Rows)
{
table1.ImportRow(dr);
}
foreach (DataRow dr in table6.Rows)
{
table1.ImportRow(dr);
}
CreateCSVFile(table1, @"C:\Program Files\Normalization\RackMap.csv");
I feel this is clunky and not very scalable but I had trouble dealing with the headers when I tried to append at the CSV level. Any suggestions?
TIA
Get a DirectoryInfo of all files matching the mask
*.csvCreate a for loop to iterate the results.
Drop the first row when importing each file.
EDIT:
If you just want to combine the files, rather than import into a data table, you could treat them as text files. Concatenate them, dropping the header line each time. Here is an example: