I currently have a program where I am taking existing log files from a directory, running through each one and calculating statistics, then creating a new file from that.
The log files are currently named as such: 2012-4-24.log
Upon creation of the new statistics file, it keeps similar formatting but adds “-Stats”: 2012-4-24-Stats.log
Here is what I am trying to solve: I want to create new directories for each year, and month, that way I can have a folder structure as follows:
DailyStatistics -> 2012 -> 4 -> 2012-4-24-Stats.log
DailyStatistics -> 2012 -> 5 -> 2012-5-1-Stats.log
etc, etc
I am not sure how to parse the year and month from the filename, and then run through all files with that same year and month, then have my program go through my loop to create the actual files.
Here is the code I have…
class Processor
{
public void ProcessDailyLogFiles()
{
foreach (string fullFileName in Directory.GetFiles(Settings.LogPath))
{
string fileName = Path.GetFileNameWithoutExtension(fullFileName);
new DailyReader().CalculateStatistics(fileName);
new DailyReader().MoveLogFile(fileName);
}
}
}
next class…
public void CalculateStatistics(string filename)
{
string path = Settings.DailyPath;
if (!path.EndsWith(@"\"))
path = path + @"\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string tempFileName = Settings.LogPath + filename + ".log";
string destFileName = path + filename + "-Stats.log";
var statistics = File.ReadLines(tempFileName)
.Where(line => line.StartsWith("Process"))
.Select(line => line.Split('\t'))
.GroupBy(items => items[1])
.Select(g =>
new
{
Division = g.Key,
ZipFiles = g.Sum(i => Convert.ToInt32(i[4])),
Conversions = g.Sum(i => Convert.ToInt32(i[5])),
ReturnedFiles = g.Sum(i => Convert.ToInt32(i[6])),
TotalEmails = g.Sum(i => Convert.ToInt32(i[7]))
});
Log myLog = new Log(destFileName);
statistics
.ToList()
.ForEach(d => myLog.Write(d.Division, d.ZipFiles, d.Conversions, d.ReturnedFiles, d.TotalEmails));
//Add error handlers
myLog.Close();
}
final log class…
#region Member Variables
StreamWriter dailyStats;
#endregion
public Log(string filename)
{
this.Open(filename);
}
#region Public Static Functions
public void Open(string tempFileName)
{
var sb = new StringBuilder();
if (!File.Exists(tempFileName))
{
dailyStats = File.AppendText(tempFileName);
sb.Append("Division");
sb.Append("\t");
sb.Append("Zip Files");
sb.Append("\t");
sb.Append("Conversions");
sb.Append("\t");
sb.Append("Returned Files");
sb.Append("\t");
sb.Append("Total E-Mails");
sb.Append("\t");
dailyStats.WriteLine(sb.ToString());
}
else
{
dailyStats = new StreamWriter(tempFileName);
}
}
public void Write(string division,
int zipFiles, int conversions, int returnedFiles, int totalEmails)
{
var sb = new StringBuilder();
if (writeLog)
{
sb.Append(division);
sb.Append("\t");
sb.Append(zipFiles);
sb.Append("\t");
sb.Append(conversions);
sb.Append("\t");
sb.Append(returnedFiles);
sb.Append("\t");
sb.Append(totalEmails);
sb.Append("\t");
dailyStats.WriteLine(sb.ToString());
}
}
public void Close()
{
dailyStats.Close();
}
}
Thanks guys
1 Answer