hey friends i am reading a csv file and i have to read a specifice coloumn form the file and that coloumn contains date and i have to read the date first and then keep a validation that if the dates in the file are between some dates then the program will read data from another file . The csv file contains a header and that creates a problem while reading the first row the file the file data is like this:

start_date_time,ani,dialed_digits,actual_dur,rounded_dur_secs,cost
"03/01/2008 00:05:57",629172162448,"923455755684",2,2,0.002800
"03/01/2008 00:15:56",79279906564,"79278454880",51,60,0.135000
"03/01/2008 00:16:51",4166143724,"92922202502",188,188,0.256900
"03/01/2008 00:23:13",07956563557,"925871021085",1020,1020,1.393400
"03/01/2008 00:38:13",639262060046,"923084230440",1,1,0.001400
"03/01/2008 00:47:02",--------------,"92945623075",124,124,0.169400
"03/01/2008 01:08:14",7187696383,"92946725834",457,457,0.624300

The code of mine goes here

static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\ric_mar.csv");
            StreamWriter sw = new StreamWriter(@"C:\dates.txt");

            string dates = "";
            string[] dvalue = null;
            IList<string> list = new List<string>();
            while (!sr.EndOfStream)
            {
                dates = sr.ReadLine();
                dvalue = dates.Split(',',';');
                if (dvalue.Length > 3)
                {
                    list.Add(dvalue[0].Replace("\"",""));
                }
            }
            DateTime begin = new DateTime(2008, 3, 1);
            DateTime end = new DateTime(2008, 3, 9);

           
            foreach (string var in list)
            {
                DateTime datevalue = DateTime.ParseExact(dvalue[0].ToString(), "3/1/2008 00:00:00", null);
                //DateTime datevalue = DateTime.Parse(var);
                Console.WriteLine(datevalue.ToString());
                Console.ReadLine();
                sw.WriteLine(var.ToString());
            }         
            sw.Flush();
            sw.Close();
        }
        
    }
}

Recommended Answers

All 3 Replies

If you managed to read the date column data so put them in array
2- remove the ' " ' from each string(date) using string.Remove("\""); method
3- convert the string to date and compare the two dates DateTime.Compare(date1, date2);
4- based on value returns from DateTime.Comapre -1, 0 or 1 go through execution some codes...

hi ramy i have written the code like this have a look at it:

StreamReader sr = new StreamReader(@"C:\Documents and Settings\jitendra\Desktop\Copy of ric_mar.csv");
            StreamWriter sw = new StreamWriter(@"C:\dates.txt");

            string dates = "";
            string[] dvalue = null;
            IList<string> list = new List<string>();
            IList<DateTime> mydd = new List<DateTime>();
            while (!sr.EndOfStream)
            {
                dates = sr.ReadLine();
                dvalue = dates.Split(',',';');
                if (dvalue.Length > 3)
                {
                    list.Add(dvalue[0].Replace("\"",""));
                }
            }
            foreach (string var in list)
            {
                DateTime datevalue = DateTime.ParseExact(var, "MM/dd/yyyy HH:mm:ss", null); 
                sw.WriteLine(var.ToString());    
            }

this prints the date in perfect manner now what i want is that if the array contains the date value between 03/01/2008 (mmddyyyy) to 03/09/2008 then the program should print the whole records which come in between these dates. so can you help me please..

Option #1

private void button2_Click(object sender, EventArgs e)
        {
            string sLine = "";
            DateTime dates;
            DateTime start = Convert.ToDateTime("03/01/2008 00:00:00");
            DateTime end = Convert.ToDateTime("03/09/2008 00:00:00");
            StreamReader sr = new StreamReader(@"C:\cvsParse.txt");
            StreamWriter sw = new StreamWriter(@"C:\dates.txt");

            sw.WriteLine( sr.ReadLine() );
            while (!sr.EndOfStream)
            {
                sLine = sr.ReadLine();
                dates = Convert.ToDateTime(sLine.Replace("\"",string.Empty).Split(',')[0]);
                if (dates >= start && dates < end)
                    sw.WriteLine(sLine);
            }
            sr.Close();
            sw.Flush();
            sw.Close();

        }

Option #2

private void button1_Click(object sender, EventArgs e)
        {
            string header = "";
            string dates = "";
            string[] dvalue = null;

            StreamReader sr = new StreamReader(@"C:\cvsParse.txt");
            StreamWriter sw = new StreamWriter(@"C:\dates.txt");

            DataTable dt = new DataTable("cvs");
            dt.Columns.Add("start_date_time", typeof(DateTime));
            dt.Columns.Add("origin");
            header = sr.ReadLine();
            while (!sr.EndOfStream)
            {
                dates = sr.ReadLine();
                dvalue = dates.Split(',');
                dt.Rows.Add(Convert.ToDateTime(dvalue[0].Replace("\"", string.Empty))  , dates );
            }
            sr.Close();

            DataRow[] dr = dt.Select("start_date_time > '03/01/2008 00:00:01' and start_date_time < '03/01/2008 00:30:59'");
            sw.WriteLine(header);
            foreach (DataRow row in dr)
                sw.WriteLine(row["origin"].ToString());

            sw.Flush();
            sw.Close();

        }

Both examples create a new file with only the desired dates.

Option #1 simply copys the file into a new file where the date is in the desired range .
Option #2 is a little fancier, where it uses a DataTable to get the data, and allows you to write it out at one time. The datatable approach opens the door to all kinds of filtering, sorting, write out as XML, whatever you can do with a data table.

// Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.