I have the code below that searches a directory (z:), it returns all the files for yesterday, I need to read through this list of today files then line by line and search for any error messages, if I find any error messages in a file, I want to write out the file name followed by the error message that is in the file, the error can contain different nos of lines.
The first issue I am having is when I try to read the list of today files with File.ReadLines(file); it doesn’t like this and then I also need to format writing the file name followed by the error message and the different no of lines it can contain.
The files are in the .htm format.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace NetworkSearch
{
class Program
{
static void Main(string[] args)
{
try
{
DateTime today = DateTime.Now.AddDays(-1);
FileInfo[] todaysFiles = new DirectoryInfo(@"z:\")
.EnumerateFiles()
.Select( x => {
x.Refresh();
return x;
})
.Where( x => x.CreationTime.Date == today || x.LastWriteTime >= today )
.ToArray()
;
string fileName = @"D:\Temp\Network.txt";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
using (StreamWriter sw = File.CreateText(fileName))
foreach (FileInfo file in todaysFiles)
{
File.ReadLines(file);
if (line.Contains("ERROR"))
{
sw.WriteLine("----- ERROR -----");
sw.WriteLine(file); //file name
sw.WriteLine(errLine); //errline can contain diff nos of lines
sw.WriteLine("----- ERROR -----");
}
}
}
catch (UnauthorizedAccessException UAEx)
{
Console.WriteLine(UAEx.Message);
}
catch (PathTooLongException PathEx)
{
Console.WriteLine(PathEx.Message);
}
}
}
}