Question

I'm writing a program that iterates a file tree and now I need it to write the results to a csv file. It's required that the contents be sorted alphanumerically before being written to the csv file. So I have been saving the files to a List.

FileInfo fi = new FileInfo(file);
fileList.Add( new Record {
    fileName = fi.Name,
    fileSize = fi.Length
});

Where Record is a class:

public class Record
{
    public long fileSize { get; set; }
    public string fileName { get; set; }

}

Then I'm doing a plain old Sort(). Now how can I write this list to a csv? I welcome any better ways to do the whole process.

Is there a way that I can write the results to the csv as I go and then sort the csv alphanumerically? Basically the program has to sort it and not the user.

The csv also needs headers if you could get me going on that as well.

Was it helpful?

Solution

You can use LINQ for the sorting and a StreamWriter for the file creation.

using (var writer = new StreamWriter(@"C:\files.csv")) {
    writer.WriteLine("Name,Size"); // Header
    var query = fileList.OrderBy(r => r.fileName);
    foreach (Record record in query) {
        writer.WriteLine("\"{0}\",{1}", record.fileName, record.fileSize);
    }
}

Note that I enclosed the file name in double quotes ("), in the case that a comma (,) is part of the file name. The double quote itself is not a valid character for file names in Windows, so that it should not be a problem.

OTHER TIPS

As long as you can fit all of the data in memory you're better off holding onto it all and sorting it in memory rather than writing it to a file. The concept of sorting data in a file is generally solved by "read it into memory, sort it, and write it back out again".

As for writing a CSV file, it's not really that hard. I like to use a StringBuilder to add on each of the fields for each line, and then once I have a line I append that line to the file. For the headers, you either hard code them if appropriate, or get them from whatever source you have if it's dynamic. Do you not know how to write to a file? My guess is the File.WriteAllText() and File.AppendText() methods will be all you should need for that.

Just a general tip, since you asked, is that rather than adding the items to the list, since you'll eventually sort them all, is to add them to a SortedList, so that they're sorted as you go. Then when you're done, you just get them one at a time and they are already in order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top