Gmac
Gmac

Reputation: 169

VB - If StreamReader reads line containing ("string1") but missing ("string2")

EDIT: How would I go about retrieving the filename of the files that meet said conditions?

How I can check if a string contains "value1" but doesn't have "value2"? I tried this:

While Not sr.EndOfStream
    Dim sLine As String = sr.ReadLine
    If sLine.Contains("value1") Then
        If Not sLine.Contains("value2") Then
            sw.WriteLine("write name of file that meets conditions to txt file")
End While

Not sure how to go about searching for the missing value.

Upvotes: 0

Views: 385

Answers (2)

the_lotus
the_lotus

Reputation: 12748

You could load the whole file in a string and do the check

If wholeFileData.Contains("value1") AndAlso Not wholeFileData.Contains("value2") Then
    sw.WriteLine("write name of file that meets conditions to txt file")
End If

If you are need to loop each line, then you'll need to store in a variable and show the message at the end.

Dim containsValue1, containsValue2 As Boolean

containsValue1 = False
containsValue2 = False

While Not sr.EndOfStream
    Dim sLine As String = sr.ReadLine

    If sLine.Contains("value1") Then
        containsValue1 = True
    End If

    If sLine.Contains("value2") Then
        containsValue2 = True
    End If
End While

If containsValue1 AndAlso Not containsValue2 Then
    sw.WriteLine("write name of file that meets conditions to txt file")
End If

Upvotes: 3

Tony Hinkle
Tony Hinkle

Reputation: 4742

If sLine.Contains("value1") = True and sLine.Contains("value2") = False Then
    Msgbox("sLine contains value1, but does not contain value2")
End If

Upvotes: 0

Related Questions