Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using csvHelper in filtering csv files. I had a code below but I don't know how to process the filtered out records in if statements. I want to write it again only the lines within the date range.

What I have tried:

var dFm = new DateTime(2015, 01, 25);
var dTo = new DateTime(2015, 01, 27);
using (var reader = File.OpenText(@"sample.csv"))
{
    var csv = new CsvReader(reader);
    while (csv.Read())
    {
        var date = csv.GetField<DateTime>("Date");
        if (date >= dFm &&
            date <= dTo)
        {
            // process the filtered out records
        }
    }
}
Posted
Updated 19-Jun-17 23:43pm
v3
Comments
Tomas Takac 20-Jun-17 2:19am    
There is a CsvWriter class, try to use that. You need to write to another than source file though.
Member 13264296 20-Jun-17 3:02am    
There's no other way to re-write the data with same directory?
Richard MacCutchan 20-Jun-17 3:31am    
No, you cannot overwrite the same file that you are currently reading.
Member 13264296 20-Jun-17 3:56am    
This is not good in conserving memory specially if we have a lot of unique csv filenames.
Richard MacCutchan 20-Jun-17 4:17am    
This has nothing to do with conserving memory.

I never used CsvHelper, but on visiting the Issues on the GitHub page I noticed an issue:
Multiple date format support · Issue #603 · JoshClose/CsvHelper · GitHub[^]

After reading that I think for dates might you need to use something like:
C#
TypeConverterOption("yyyyMMdd")

Updated answer:
C#
var dFm = new DateTime(2015, 01, 25);
            var dTo = new DateTime(2015, 01, 27);

            using (var reader = File.OpenText(@"sample.csv"))
            {
                var csv = new CsvReader(reader);

                while (csv.Read())
                {
                    var date = csv.GetField<string>("Date");
                    // See: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings
                    var date2 = DateTime.ParseExact(date, "MM/dd/yyyy", null);

                    if (date2 >= dFm && date2 <= dTo)
                    {
                        // process the filtered out records
                        Debug.Print(date2.ToLongDateString());
                    }
                }
            }
 
Share this answer
 
v3
Comments
Member 13264296 20-Jun-17 5:51am    
SAMPLE INPUT:
"EmployeeCode","Date","Time","Type"
"3434","01/22/2013","07:54","0"
"3023","01/23/2014","07:54","0"
"2897","01/24/2015","07:54","0"
"3734","01/25/2015","07:54","0"
"3168","01/26/2015","07:54","0"
"4863","01/26/2015","07:55","0"
"2513","01/27/2015","07:55","0"
"2582","01/27/2015","07:55","0"


OUTPUT:
"EmployeeCode","Date","Time","Type"
"3734","01/25/2015","07:54","0"
"3168","01/26/2015","07:54","0"
"4863","01/26/2015","07:55","0"
"2513","01/27/2015","07:55","0"
"2582","01/27/2015","07:55","0"
I think what you need is to appreciate that a csv file initially is simply 'a file of Comma Seperated Verdices'. In detail actually it's not necessarily seperated by Comma, it is seperated on the thread by the value set in System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator

Once you think of that, getting the value out of what you have read and treated with any sort of reader should be straight forward. If you're using the open source CSV reader from Github and it doesn't support what you want, simply fork and extend it to be able.

In essence you're going to be writing to a stream that binds to the same file you've been reading, just remember to close it before writing and it can hardly go wrong :)
 
Share this answer
 
Comments
Member 13264296 20-Jun-17 4:40am    
For me as a beginner. I need some examples and codes for me to understand clearly.
Thomas Nielsen - getCore 20-Jun-17 7:46am    
For interacting with text files, for instance: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file

plus if u want this one can read and write: https://github.com/JoshClose/CsvHelper

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