[Solved] search for string in text file in vb and print lines


A very bad formed question for this site. It’s good for you to spend some time to read the rules.

Anyway, this is from me.

Const ForReading = 1, ForWriting = 2
Dim FSO, FileIn, FileOut, strTmp

Set FSO     = CreateObject("Scripting.FileSystemObject")
Set FileIn  = FSO.OpenTextFile("shar.txt", ForReading)
Set FileOut = FSO.OpenTextFile("sha.txt", ForWriting, True)

Do Until FileIn.AtEndOfStream
    strTmp = FileIn.ReadLine
    If Len(strTmp) > 0 Then
        If InStr(1, strTmp, "Friends", vbTextCompare) > 0 _
        Or InStr(1, strTmp, "support", vbTextCompare) > 0 Then
            FileOut.WriteLine strTmp
        End If
    End If
Loop

FileIn.Close
FileOut.Close

EDIT: About your question for using arrays…

' an example array
arWords = Array("friends", "support", "xyz")
' modified Do..Loop
Do Until FileIn.AtEndOfStream
    strTmp = FileIn.ReadLine
    If Len(strTmp) > 0 Then
        For i = 0 To UBound(arWords)
            If InStr(1, strTmp, arWords(i), vbTextCompare) > 0 Then
                FileOut.WriteLine strTmp
                Exit For
            End If
        Next
    End If
Loop

Cheers!

6

solved search for string in text file in vb and print lines