Reputation: 1455
I have a text file which is getting populated continuously from the server data.Here is the sample data present in the text file..
1/1/2014 234 34 456788889989 23
1/1/2014 234 34 456788889989 23
1/1/2014 234 34 456788889989 23
2/1/2014 234 34 456788889989 23
2/1/2014 234 34 456788889989 23
2/1/2014 234 34 456788889989 23
3/1/2014 234 34 456788889989 23
3/1/2014 234 34 456788889989 23
3/1/2014 234 34 456788889989 23
Here is my code to create text file..
System.IO.File.WriteAllText(@"D:\datewise.txt", "");
Here for every date there is lot of data present as row.Now as soon as date changes i have to create a text file and copy all the data from sample text file to the newly created text file.How to achieve this?
Upvotes: 1
Views: 943
Reputation:
using System.Text.RegularExpressions;
using System.IO;
using System.Text;
string pattern = "^3/1/2014.*";
string strPath = new string("c:\\");
string strDateTime = DateTime.Now.ToString("yyyyMMdd");
string FileToCopy = "c:\\regexTest.txt";
string NewCopy = strPath + strDateTime + ".txt";
StringBuilder sb = new StringBuilder("");
if (System.IO.File.Exists(FileToCopy) == true) {
string[] lines = File.ReadAllLines(FileToCopy);
foreach (string line in lines) {
if (Regex.IsMatch(line, pattern)) {
sb.Append(line + System.Environment.NewLine);
}
}
}
if (sb.Length > 0) {
System.IO.File.WriteAllText(NewCopy, sb.ToString);
}
Upvotes: 1