Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Routine to search text files and list the file found and the lines the matches are found on.

Posted on 2004-10-06
5
Medium Priority
?
152 Views
Last Modified: 2010-05-02
Hi
I need help from the Guru's!
I have a lot of text files and I need to be able to do a text file search for matching words or phrases.

I need to list the names of the files where matches were found along with the contents of the line where the match was found. Like this:

Search term: Holiday

File1.txt
Line 23: it was time for a holiday with the staff
Line 340:  Arnold said he needed a holiday

File2.txt
Line 72: We all need a holiday!

End of Search

Could anyone point me to a (preferably vb script) routine or code for this?

Thank you!

Ted
0
Comment
Question by:tcalbaz
5 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12240244
Try this

Option Explicit

Private Sub Command1_Click()

End Sub
Private Sub Readfile(ByVal Filename As String, ByVal matchstring As String)
    Dim ff As Integer, s As String, arr, i As Long
       
    ff = FreeFile
    Open Filename For Binary Access Read Lock Read Write As #ff
    s = Space$(LOF(ff))
    Get #ff, , s
    Close ff
   
    Debug.Print Filename
    arr = Split(s, vbCrLf)
    For i = 1 To UBound(arr)
        If (InStr(1, ucase(arr(i - 1)), ucase(matchstring))) Then
            Debug.Print "Line " & i + 1 & ": " & arr(i - 1)
        End If
    Next
   
    Erase arr
End Sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12240568
Create a new project and add a class.  To the form, add a commandbutton, label and a textbox.  For the textbox, set the MultiLine property to true and the Scrollbars property to 3 (both).  Paste the code below into the appropriate area as labeled:

Change the call to the searchFiles sub in Command1_Click to meet your needs.

Regards,

Idle_Mind

' --------------------------------------------------------
' Class1
' --------------------------------------------------------
Public fileName As String
Public lineNumbers As Collection
Public lines As Collection

Private Sub Class_Initialize()
    Set lineNumbers = New Collection
    Set lines = New Collection
End Sub

' --------------------------------------------------------
' Form1
' --------------------------------------------------------
Option Explicit

Private matches As Collection

Private Sub Form_Load()
    Label1.Caption = ""
End Sub

Private Sub Command1_Click()
    Dim match As Class1
    Dim lineNumber As Variant
   
    Set matches = New Collection
    searchFiles "C:\Documents and Settings\Michael\My Documents\1 VB Code\", "*.txt", "test"
    Text1.Text = ""
    For Each match In matches
        Text1.Text = Text1.Text & match.fileName & vbCrLf
        For Each lineNumber In match.lineNumbers
            Text1.Text = Text1.Text & "Line " & lineNumber & ": " & match.lines.Item(CStr(lineNumber)) & vbCrLf
        Next
        Text1.Text = Text1.Text & vbCrLf
    Next
    Label1.Caption = matches.Count & " file(s) with matches found"
End Sub

Private Sub searchFiles(ByVal basePath As String, ByVal filter As String, ByVal value As String)
    Dim curFile As String
    Dim subdir As Variant
    Dim subDirs As Collection
    Dim ff As Integer
    Dim inputLine As String
    Dim lineNumber As Long
    Dim match As Class1
   
    curFile = Dir(basePath & filter)
    Do While curFile <> ""
        Label1.Caption = basePath & curFile
        Set match = Nothing
        lineNumber = 0
        ff = FreeFile
        Open basePath & curFile For Input As #ff
        While Not EOF(ff)
            lineNumber = lineNumber + 1
            Line Input #ff, inputLine
            If InStr(LCase(inputLine), LCase(value)) > 0 Then
                If match Is Nothing Then
                    Set match = New Class1
                    match.fileName = basePath & curFile
                End If
                match.lineNumbers.Add lineNumber
                match.lines.Add inputLine, CStr(lineNumber)
            End If
            DoEvents
        Wend
        Close #ff
        If Not (match Is Nothing) Then
            matches.Add match
        End If
       
        DoEvents
        curFile = Dir()
    Loop
   
    ' build subDirs collection
    Set subDirs = New Collection
    curFile = Dir(basePath, vbDirectory)
    Do
        If curFile <> "" Then
            If (GetAttr(basePath & curFile) And vbDirectory) = vbDirectory Then
                If curFile <> "." And curFile <> ".." Then
                    subDirs.Add basePath & curFile & "\"
                End If
            End If
        End If
        DoEvents
        curFile = Dir()
    Loop While curFile <> ""
   
    ' recurse into each subdir
    For Each subdir In subDirs
        searchFiles subdir, filter, value
    Next
End Sub
0
 
LVL 6

Accepted Solution

by:
SCDMETA earned 1500 total points
ID: 12256124

I realize you are asking for code to do this...
but just in case you are trying to code your way out of a tedious task I thought I would point you towards the following program.  It allows standard and regular expression search, replace, etc across multiple files.

http://www.wingrep.com/
0
 
LVL 1

Author Comment

by:tcalbaz
ID: 12300669
SCDMETA  

This is the answer which proves most useful.

Thank you

Ted
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12300787
< Sighs >

>> Could anyone point me to a (preferably vb script) routine or code for this?

For future reference tcalbaz...

If you are going to ask a question in a PROGRAMMING topic area asking for code, then at least give those who gave you actual code some Assist points if you accept a non coding solution.

No offense meant to SCDMETA as WinGrep looks like a great utitlity.

=)

~IM
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question