Tamal Banerjee

Tamal Banerjee

  • NA
  • 25
  • 3.3k

How to find which files have errors according to the pattern

Sep 5 2016 11:28 AM
I want to search some regex patterns in files (*.txt) which are inside a folder whose path I'have given in a text box, and the folder contains other sub-folders with txt files in the form 12345-2031-30201\2031\30201\txt\110.txt and if the pattern matches even in one file, then a string is written on a log file which is created inside the folder whose path I've given in the text box and then it moves on to the next regex and so on.
 
The problem I'm having is the log file writing the matches found in the form
"D:\test\bk\1235-12-3053\230\124\txt\124.txt: Check table link, Check section link, Check figure link "
"D:\test\bk\123561-1-2356\230\129\txt\129.txt: Check section link, Check table link"
However I want it show the macthes found in the below fashion
"Check figure link"==
D:\test\bk\1235-12-3053\230\124\txt\124.txt
D:\test\bk\1235-12-3053\230\131\txt\131.txt
"Check table link"==
D:\test\bk\1235-12-3053\230\124\txt\124.txt
D:\test\bk\1235-12-3053\230\205\txt\205.txt
and so on
 
  1. Dim patterns = New List(Of String()) From {  
  2.      ({"Check figure link""(?<!>)(?:figures?|figs?\.) \d+"}),  
  3.      ({"Check table link""(?<!>)(?:tables?|tabs?\.) \d+"}),  
  4.      ({"Check section link""(?<!>)(?:sections?|sect?\.) \d+"}),  
  5.      ({"Check space""</inline>\w+"})}  
  6.   
  7.         Dim compiledPatterns = New Dictionary(Of Regex, String)  
  8.         For Each pat As String() In patterns  
  9.             compiledPatterns.Add(New Regex(pat(1), RegexOptions.Compiled), pat(0))  
  10.         Next  
  11.   
  12.         Dim filteredFilenames = From tFile In Directory.EnumerateFiles(TextBox1.Text, "*.txt", SearchOption.AllDirectories)  
  13.                                 Where tFile Like "*\#*\#*\#*\txt\#*.txt"  
  14.   
  15.         Dim output = From tFile In filteredFilenames.AsParallel  
  16.                      Let checks = CheckFile(tFile, compiledPatterns)  
  17.                      Where checks.Any  
  18.                      Select Path = tFile, Messages = checks  
  19.   
  20.         File.WriteAllLines(TextBox1.Text.TrimEnd("\"c) & "\Checklist.log",  
  21.                    From fm In output Select  
  22.                      fm.Path & ": " & String.Join(", ", fm.Messages))  
  23.         MsgBox("Process Complete")  
 Can anybode help?

Answers (1)