Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

704 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