-3

I have a folder save here: C:\MyFiles

And in that folder I have a few .txt files.

I am looking to see what would be the best way to open each .txt file in that folder and search for the value Error or Warning and then when it does it must tell me what files do contain those words.

What would be my best option? To create a Stored Procedure with t-sql to do this? or To create a .exe with C# or VB.net code.

Anyone have an example for me to look at to get started?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Etienne
  • 7,141
  • 42
  • 108
  • 160
  • Why a stored procedure? You're working with text files, not with a database. It depends what you have to do with that information. If you just need a search tool...even a **batch file** could be enough (take a look to `FINDSTR` command). – Adriano Repetti Jun 27 '12 at 08:39
  • To have a stored procedure you have to have the data in a database, which you don't, do you? What's the size of the files in question? – t3hn00b Jun 27 '12 at 08:39
  • Well all the developers here are SQL Developers so it would be easy for them to understand. The files are small, all of them only have about 100 lines of data and there will only be between 5 and 20 files in the folder. – Etienne Jun 27 '12 at 08:40
  • tsql work primally on sql server database engine. A stand alone application sound the best option – Angelo Badellino Jun 27 '12 at 08:41
  • Just create a simple c# application. – t3hn00b Jun 27 '12 at 08:42
  • Anyone have an example of code that I can use to get started? – Etienne Jun 27 '12 at 08:42
  • 1
    "it would be easy for them to understand" - A well-written application in any language should meet that criteria. Otherwise I'd hesitate to call those involved "developers." `"Clean code reads like well-written prose." - Grady Booch` – David Jun 27 '12 at 08:44
  • Powershell might be the quickest and easiest option here. – Goody Jun 27 '12 at 08:44
  • If you're just looking for C# code to read a file line-by-line, you won't find much better than this: http://stackoverflow.com/a/1271236/328193 – David Jun 27 '12 at 08:45
  • 1
    Why not just run `find /c "Error" C:\MyFiles\*.txt` and then run `find /c "Error" C:\MyFiles\*.txt`? You could possibly pipe the output through `findstr`, if necessary. I'm no expert on the available command line tools, but I'm sure there's ways of getting close to what you want with a simple batch file rather than having to write your own application that reinvents the wheel. – Steven Doggart Jun 27 '12 at 12:20
  • Thanks SteveDog, I will use this! Add it to an answer below and I will mark you as accepted. But can you please add it to search for the 2 words at the same time and only run once? – Etienne Jun 29 '12 at 05:54

3 Answers3

0

C# Console Application is the way to go!

  1. Get files from directory
  2. Read text from a file
aF.
  • 64,980
  • 43
  • 135
  • 198
0

This is a sample project, doesn't use advanced functionality to keep simple and comprensible

using System;
using System.IO;
using System.Text;

namespace CheckErrorWarning
{
    class Program
    {
        // Pass on the command line the full path to the directory with the txt files
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: CheckErrorWarning <directoryname>");
                return;
            }
            if (Directory.Exists(args[0]) == false)
            {
                Console.WriteLine("Directory : " + args[0] +" not found or not accessible");
                return;
            }
            string[] textFiles = Directory.GetFiles(args[0], "*.txt");
            foreach (string fileName in textFiles)
            {
                string[] lines = File.ReadAllLines(fileName);
                for (int x = 0; x < lines.Length; x++)
                {
                    int warnPos = lines[x].IndexOf("warning",
                                  StringComparison.CurrentCultureIgnoreCase);
                    if (warnPos > 0) Console.WriteLine("File:" + fileName + 
                                     ", warning at line " + x + 
                                     ", position " + warnPos);
                    int errPos = lines[x].IndexOf("error", 
                                 StringComparison.CurrentCultureIgnoreCase);
                    if (errPos > 0) Console.WriteLine("File:" + fileName + 
                                    ", error at line " + x + 
                                    ", position " + errPos);
                }
            }
        }
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Where do I specify to look at "C:\MyFiles" ? – Etienne Jun 27 '12 at 09:06
  • on the command line. Compile the sample and type on a dos window the name of the program followed by the name of the directory: For example `"C:\> CheckErrorWarning C:\MyFiles"` – Steve Jun 27 '12 at 09:08
  • I want to create an .exe so the users can just run the .exe – Etienne Jun 27 '12 at 09:43
  • 1
    Sorry, but I don't get it. To create an exe you need the Visual Studio (also the [Express](http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express) free edition). Then create a new C# Console application project, replace the prewritten code with the one above and choose Compile Solution from the menu. You will find your exe in the Debug/Relase subfolder of your project. – Steve Jun 27 '12 at 09:50
  • I have done all of this and I get my .exe but when I run it nothing happens. Sorry I have never created .exe's and I dont know C# that well. – Etienne Jun 27 '12 at 11:18
  • If you run the program inside the Visual Studio IDE then select project properties and set, in the Debug Page -> Command Line Arguments, your directory. Also to see the flow of the program start the program with the debugger (F11) (Sorry I have a localized version of VS and don't know how to translate the exact command - Execute Instruction perhaps) – Steve Jun 27 '12 at 11:51
0

I ended up creating a BAT file using this command.

find /c "error" C:\MyFiles\*.txt
find /c "warning" C:\MyFiles\*.txt
Etienne
  • 7,141
  • 42
  • 108
  • 160