Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button2_Click(object sender, EventArgs e)
      {
        DateTime start = new DateTime(01 / 25 / 2015);
        DateTime end = new DateTime(01 / 27 / 2015);
     
        var dates = new List<datetime>();
    
        for (var dt = start; dt <= end; dt = dt.AddDays(1))
        {
          dates.Add(dt);
    
          var oldLines = System.IO.File.ReadAllLines(txtFileName.Text);
          var newLines = oldLines.Where(line => 
          line.Contains(dates.ToString()));          
          System.IO.File.WriteAllLines(txtFileName.Text, newLines);
    
          newLines = oldLines.Select(line => new
    
            {
                Line = line,
                Words = line.Split(' ')
            })
             .Where(lineInfo => lineInfo.Words.Contains(dates.ToString()))
             .Select(lineInfo => lineInfo.Line);
         }
      }

After browsing the file from directory.
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"


Suggestions, comments, sharing codes and ideas are highly appreciated.

What I have tried:

To pursue my goal of deleting multiple lines in csv file. My idea is to get the dates between two dates that are not include for deletion. But how can I iterate days between two dates and get there values to string and used to my code for evaluating the csv file that contains this field.
Posted
Updated 30-Jun-17 10:58am
v4
Comments
Maciej Los 17-Jun-17 4:18am    
What is exact input? Seems, you want to split data by [" "] (space), but the example data you posted is delimited by comma [","].
Member 13264296 17-Jun-17 4:29am    
It's fine, there's no problem by that issue, it will just overwritten with empty string. My problem is how can I iterate days between two dates including months,days and year, then the values will insert to this code .Contains("date values here").
Maciej Los 17-Jun-17 4:40am    
Check my solution.

I think your DateTime statement is wrong and should look like this:
C#
DateTime start = new DateTime(2015, 1, 25);
Console.WriteLine("Date = " + start);
Console.WriteLine("Date = " + start.ToShortDateString());
Will give:
Date = 1/25/2015 12:00:00 AM
Date = 1/25/2015

Updated Example:
C#
using System;
using System.Linq;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		DateTime start = new DateTime(2015, 1, 25);
		DateTime end = new DateTime(2015, 1, 27);
		
		var dates = new List<DateTime>();
		var oldLines = new List<string> { "01/25/2015", "01/26/2015", "01/27/2015", "01/28/2015" };
		var newLines = new List<string>();
		
		for (var dt = start; dt <= end; dt = dt.AddDays(1))
        {
        	dates.Add(dt);
		}
		
		foreach(var d in dates)
		{
			newLines.AddRange(oldLines.Where(line => line.Contains(d.ToShortDateString())));
		}
		
		Console.WriteLine(newLines.Count());
	}
}
 
Share this answer
 
v3
Comments
Member 13264296 17-Jun-17 4:34am    
How can I used it to my code?
Member 13264296 17-Jun-17 4:57am    
That is only one row.
RickZeeland 17-Jun-17 5:02am    
Yep :) you will have to finish the code yourself ...
Member 13264296 17-Jun-17 5:08am    
hehe. okay but you didn't iterates dates between two dates.
Member 13264296 18-Jun-17 21:01pm    
What if the date is from the 2 datetimepicker.
Assuming that data are delimited by space...

Assuming that data are arounded by double quote and delimited by comma...

C#
string[] lines = {@"""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"""};

DateTime dFm  = new DateTime(2015, 1, 25);
DateTime dTo  = new DateTime(2015, 1, 27);

var data = lines
	.Skip(1)
	.Select(x=> new
	{
		EmpCode = Int32.Parse(x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[0]),
		Date = DateTime.ParseExact(
			string.Concat(x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[1], " ",
				x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[2]),
			"MM/dd/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture),
		Type = x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[3]
	})
	.Where(x=>x.Date>=dFm && x.Date<=dTo)
	.ToList();


Result:
3734 2015-01-25 07:54:00 0 
3168 2015-01-26 07:54:00 0 
4863 2015-01-26 07:55:00 0 


Good luck!
 
Share this answer
 
v3
Comments
Member 13264296 17-Jun-17 4:44am    
Error:The format is not like the format in the output sample. The format of my csv file is like my example input above. Time is a separate column.
Maciej Los 17-Jun-17 5:19am    
Solution has been updated.
Member 13264296 17-Jun-17 5:26am    
Error: Index was outside the bounds of the array and It seems there is no Time column in the string because it is separate column and also separate input by the user.
Maciej Los 17-Jun-17 5:31am    
I've tested above code based on your data and there's no errors.
Member 13264296 18-Jun-17 20:37pm    
Sorry for very late response for some reason. I declare lines like this. var lines = System.IO.File.ReadAllLines(txtFileName.Text);

ERROR: {"Index was outside the bounds of the array."} error CS1525: Invalid expression term '{'

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