Cody Abode
Cody Abode

Reputation: 27

Checking if any items in an array match any items in another array in Visual Basic

I have a directory with .txt files in it. I am writing each file's line to an array and need to know if any lines in a file match any lines in another file.

Example:

If any item in Array1 = any item in Array2 then...

Code thus far:

For Each foundBaseFile As String In My.Computer.FileSystem.GetFiles _
 (DataDir, _
     FileIO.SearchOption.SearchTopLevelOnly, "*.vpk.txt")
        Dim BaseTextArray = IO.File.ReadAllLines(foundBaseFile)
        For Each foundCheckFile As String In My.Computer.FileSystem.GetFiles _
 (DataDir, _
     FileIO.SearchOption.SearchTopLevelOnly, "*.vpk.txt")
            If Not foundBaseFile = foundCheckFile Then
                Dim CheckTextArray = IO.File.ReadAllLines(foundCheckFile)
                'If any item in CheckTextArray = any item in BaseTextArray then
                '    Do X
                'End If
            End If
        Next
    Next

Thanks!

Upvotes: 0

Views: 1180

Answers (2)

Vipin Maurya
Vipin Maurya

Reputation: 58

Declare two array variable

dim arr1() as string
dim arr2() as string

Read values from .txt file line b line and add to each arrarlevel. You can also split lines by vbnewline

then use

Array.Indexof() 

method to find whether values in first array string exist in other.

If indx >1 then
True
Else
False
end if

Upvotes: 0

OneFineDay
OneFineDay

Reputation: 9024

This should do the trick.

If BaseTextArray.Any(Function(o) CheckTextArray.Contains(o)) Then
  ' Do X

Upvotes: 1

Related Questions