C# - 从多个文件中解析年/月,运行程序并在子文件夹中创建新文件(年/月)

时间:2012-04-25 16:42:38

标签: c# file parsing datetime logging

我目前有一个程序,我从目录中获取现有日志文件,运行每个日志文件并计算统计信息,然后从中创建新文件。

日志文件目前的名称如下:2012-4-24.log

创建新的统计信息文件后,它会保留类似的格式,但会添加“-Stats”:2012-4-24-Stats.log

以下是我要解决的问题:我想为每年和每月创建新目录,这样我就可以拥有如下文件夹结构:

DailyStatistics - > 2012 - > 4 - > 2012-4-24-Stats.log

DailyStatistics - > 2012 - > 5 - > 2012-5-1-Stats.log

等等

我不确定如何从文件名解析年份和月份,然后运行同年和月的所有文件,然后让我的程序通过我的循环来创建实际文件。

这是我的代码......

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);
        }

    }
}

下一堂课......

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();
    }

最终日志类......

#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();
    }
}

谢谢你们

1 个答案:

答案 0 :(得分:1)

  String filename = "2012-4-24.log";
  String file = Path.GetFileNameWithoutExtension(filename);
  String[] parts = file.Split('-');
  if (parts.Length == 3)
  {
    String year = parts[0];
    String month = parts[1];
    String day = parts[2];

    Console.WriteLine(string.Format("year:{0} - month:{1} - day:{2}", year, month, day));
  }