I am taking in all .csv files from a directory, taking a few columns from each file using CSVhelper and then writing to a new .csv file. However, when I'm trying to take in the files it seems to only be taking the last file in the directory and I can't understand why. I take the files in like this:
static void Main(string[] args)
{
string sourceDirectory = @"C:\Users\SourceDirectory";
var csvFiles = Directory.EnumerateFiles(sourceDirectory, "*.csv", SearchOption.AllDirectories);
foreach (string currentFile in csvFiles)
{
readFile(currentFile);
}
}
And then perform the changes in the files as such:
public static void readFile(string currentFile)
{
using (var sr = new StreamReader(currentFile))
{
using (var sw = new StreamWriter(@"C:\Users\newFile.csv"))
{
var reader = new CsvReader(sr);
var writer = new CsvWriter(sw);
//CSVReader will now read the whole file into an enumerable
IEnumerable dataRecord = reader.GetRecords<dataRecord>().ToList();
foreach (dataRecord record in dataRecord)
{
//Choose which data values you want to keep
writer.WriteField(record.info1);
writer.WriteField(record.info2);
writer.WriteField(record.info3);
//Moves the pointer onto the next record
writer.NextRecord();
}
I can't understand why it's not taking all the files in and even more confused as to why it's only taking the last?