Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Does anyone know of a tool/solution that could be ran by task scheduler that would open a large CSV file and delete rows that have a cell value in a date column older than some numeric value?

jim_chance@yahoo.com
Posted
Updated 18-Feb-10 5:16am
v2

1 solution

I don't know of one, you could try google, but it would be easy enough to write one.

Actually here is a bit of code that could get it started
public void DoFilter(Int32 skipRows, string fileName)
{
    const RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
    Regex regex = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))", options);


    string tmpFileName = Path.GetTempFileName();
    using (var sw = new StreamWriter(tmpFileName))
    {
        using (var filestream = File.OpenText(fileName))
        {
            for (int i = 0; i < skipRows; i++)
            {
                filestream.ReadLine();
                if (filestream.EndOfStream)
                {
                    break;
                }
            }
            while (!filestream.EndOfStream)
            {
                string line = filestream.ReadLine();
                var oo = regex.Split(line);

                //process your data here
                //if(row doen't match exlusion criteria
                {
                    // write to a new file
                    sw.WriteLine(line);
                }
            }
        }
    }
    //move the new file to the old file when we are finished.
    File.Move(tmpFileName, fileName);
}
 
Share this answer
 
v2

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