2012-06-27 126 views
-3

查找值「錯誤」和「警告」我有一個文件夾保存在這裏:C:\MyFiles打開文件,並在每個文件

而在這文件夾我有幾個.txt文件。

我期待看到什麼將打開該文件夾中的每個.txt文件和搜索的值ErrorWarning的最佳途徑則當它必須告訴我怎麼做files包含這些單詞。

什麼是我最好的選擇?用t-sql創建一個存儲過程來做到這一點?或者用C#或VB.net代碼創建一個.exe文件。

任何人都有一個例子讓我着眼於開始?

+0

爲什麼存儲過程?你正在處理文本文件,而不是數據庫。這取決於你必須對這些信息做什麼。如果你只是需要一個搜索工具...甚至一個**批處理文件**可能就足夠了(看看FINDSTR命令)。 –

+0

要有一個存儲過程,您必須將數據存儲在數據庫中,而您並不知道該數據,對嗎?有問題的文件的大小是多少? – t3hn00b

+0

那麼這裏的所有開發人員都是SQL開發人員,所以他們很容易理解。這些文件很小,它們都只有大約100行數據,文件夾中只有5到20個文件。 – Etienne

回答

0

我結束了創建使用該BAT文件命令。

find /c "error" C:\MyFiles\*.txt 
find /c "warning" C:\MyFiles\*.txt 
0

這是一個示例項目,不使用先進的功能,以保持簡單和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); 
       } 
      } 
     } 
    } 
} 
+0

我在哪裏指定查看「C:\ MyFiles」? – Etienne

+0

在命令行上。編譯樣本並在dos窗口中輸入程序名稱,然後輸入目錄名稱:例如'「C:\> CheckErrorWarning C:\ MyFiles」' – Steve

+0

我想創建一個.exe,以便用戶可以運行.exe – Etienne