0

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);
             }
         }
     }
 }
Filburt
  • 17,626
  • 12
  • 64
  • 115
John
  • 85
  • 1
  • 7

2 Answers2

1
using (StreamWriter sw = File.CreateText(fileName))
{
    foreach (FileInfo file in todaysFiles)
    {
        var errorLines = File.ReadLines(file.FullName).Where(l => l.Contains("ERROR")).ToList();
        if (errorLines.Any())
        {
            sw.WriteLine("----- ERROR -----");
            sw.WriteLine(file.Name); //file name
            foreach (var errLine in errorLines)
            {
                sw.WriteLine(errLine);
            }
            sw.WriteLine("----- ERROR -----");
        }
     }
}
Guillaume
  • 12,824
  • 3
  • 40
  • 48
0

File.ReadLines return IEnumerable<string>. To get a line out of it you have to loop through. And it takes a string path as argument, not FileInfo.

using (StreamWriter sw = File.CreateText(fileName))
{
    foreach (FileInfo file in todaysFiles)
    {
        foreach(string line = File.ReadLines(file.FullName));
        {
            if (line.Contains("ERROR"))
            {
                string errLine = "my error";

                sw.WriteLine("----- ERROR -----");
                sw.WriteLine(file.Name); //file name
                sw.WriteLine(errLine);  //errline can contain diff nos of lines
                sw.WriteLine("----- ERROR -----");
            }
        }
     }
}
stefankmitph
  • 3,236
  • 2
  • 18
  • 22