Muhnamana
Muhnamana

Reputation: 1044

If Else Statement Not Triggering Correctly

Ok, the following code I'm reading in a text file and searching for a specific string in a line. If the string is found, I don't want to do anything and if it's not found, I want to do something else. Right now I have a msgbox for each condition.

The problem I have, is when string is not found, it's not triggering the msgbox. The other msgbox triggers when the string is found though.

Any ideas?

    Dim logfile() As String = System.IO.File.ReadAllLines("C:\Temp\Transfer_Log.txt")
    Dim searchstring As String = "Test_" + DateTimePicker2.Value.ToString("yyyyMMdd") + ".csv"

    For Each line As String In Filter(logfile, searchstring)
        If line.Contains("Test_" + DateTimePicker2.Value.ToString("yyyyMMdd") + ".csv") Then
            MsgBox("Do Nothing") 'THIS WORKS
        Else
            MsgBox("Append") 'THIS DOES NOT WORK
        End If
    Next

Upvotes: 1

Views: 333

Answers (1)

Kratz
Kratz

Reputation: 4330

If I'm interpreting your code correcty, Filter is a filtering funciton that is returing all lines that match the text "Test_" + DateTimePicker2.Value.ToString("yyyyMMdd") + ".csv". Then you are looping through each line and again comparing it to see if it matches "Test_" + DateTimePicker2.Value.ToString("yyyyMMdd") + ".csv". So your second message box will never be called, ever. I would suggest this,

 IF Filter(logFile, searchstring).Count > 0 Then 
      MsgBox("Do Nothing")
 Else
      MsgBox("Append")
 EndIf

According to the question, you want to know if the string exists in the file or not, and this will tell you if it does or not.

Upvotes: 2

Related Questions