Hi developers,
This is my code to copy multiple files data into a single one:
public void generateMonthReport()
{
string[] inputFilePaths = Directory.GetFiles("//Path to directory for multiple files","*.txt.2018-"+DateTime.Now.ToString("MM")+"-*");
using (var outputStream = File.Create("//Path to save the new file" + DateTime.Now.ToString("MM-yyyy") + "-Statistics.txt"))
{
foreach (var inputFilePath in inputFilePaths)
{
using (var inputStream = File.OpenRead(inputFilePath))
{
inputStream.CopyTo(outputStream);
}
}
}
}
Photo of output (please see)
https://image.ibb.co/etL5T9/Capture.jpg
in that photo there is a date highlited. That line is where a new file is being written.
The problem is that that highlited date is written different from the other and when i come to read it with regex an error is being outputted.
I tried to put that highlited line in regex101 text string and it was read as a bullet
Image : https://image.ibb.co/hF7YgU/Capture1.jpg
UPDATE AS REQUESTED :
This is my regex (multiline) :
^(?<date>[^ ]+) (?<time>[^A-Z]+) (?<errorMessage>[^[]+) \[1\] (?<programName>[^.]+)[.](?<formName>[^.]+)[.](?<event>[^ ]+)[^a-z]+(?<username>[^:]+):(?<message>[^.]+).+$"
In the hexEditor i found that the line begins with (maybe it can helps):
 -> EF BB BF
This is the code to read the data from the file :
var MyTextFileDataSet = new TextFileDataSet.TextFileDataSet();
using (var filestream = new FileStream("//path of file to read.", FileMode.Open, FileAccess.Read,FileShare.ReadWrite))
{
MyTextFileDataSet.ContentExpression = new Regex(@"^(?<date>[^ ]+) (?<time>[^A-Z]+) (?<errorMessage>[^[]+) \[1\] (?<programName>[^.]+)[.](?<formName>[^.]+)[.](?<event>[^ ]+)[^a-z]+(?<username>[^:]+):(?<message>[^.]+).+$", RegexOptions.Multiline);
MyTextFileDataSet.Fill(filestream);
}
int counterError = 0, counterFatal = 0, counterWarning = 0;
var rows = MyTextFileDataSet.Tables[0].AsEnumerable();
string errorMessage = "";
string transactionsMessage = "";
int counter = 0;
foreach (var row in rows)
{
errorMessage = row.Field<string>("errorMessage");
var date = DateTime.Parse(row.Field<string>("date"));
var name = row.Field<string>("username").Trim();
transactionsMessage = row.Field<string>("message");
string transactionsString = "";
if (transactionsMessage.Contains("generated"))
{
transactionsString = getBetween(transactionsMessage, "generated", "transactions");
}
if (transactionsString != "")
{
var transactions = Convert.ToInt32(transactionsString);
var logItem = logItems
.Where(n => n.Name == name)
.FirstOrDefault();
if (logItem == null)
{
logItems.Add(new LogItemGeneration
{
Date = date,
Name = name,
Transactions = transactions
});
}
else
{
logItem.Transactions += transactions;
}
}
switch (errorMessage)
{
case "ERROR":
counterError++;
break;
case "FATAL":
counterFatal++;
break;
case "WARN":
counterWarning++;
break;
default:
break;
}
counter++;
this line :
var date = DateTime.Parse(row.Field<string>("date"));
is giving a format exception when it comes to that line where the data is written different
If you did not understand something or want to clarify something do not hesitate to comment and i will answer :)
What I have tried:
When i changed that line like the others (manual) everything worked fine.
I tried to search for other methods on internet but failed to do so.