Compare 2 Listviews

wjburke2

Active member
Joined
Feb 3, 2009
Messages
29
Programming Experience
Beginner
Hi, All, I am kind of new to VB08 and have a problem I thought you might be able to help with. I have 2 listviews that hold FileName, Size, ModifyDate. I need to compare the contents of them. If the file exists on one but not the other highlight it in red. If Size or ModifyDate of 2 is different than listview1 need to highlight the listview2 row in yellow. My main concern is the VB.2008 does not have indexing making it hard to move around as I would in VB. I am not even sure I know how to compare the values correctly in a listview. I was thinking of the logic below but I am not VB08 fluent enough to know the correct coding.

Do while not EOC
For x = 1 to listview.count
For y = 1 to listview 2 count then
If listview1.FileName(x) > listview2.FileName(y)
Highlite listview1.row(x) Red
Next x
ElseIf listview1.FileName(x) < listview2.FileName(y) then
Highlite listview2.row(Y) Red
Next y
Else
If Listview1.Size <> Listview2. Size then or
Listview1.ModifyDate <> listiview2.modify
Highlite listview2.row(Y) yellow
Next X
Next Y
E:ondif
Endif
Loop
Loop
End while
 
Well, your two sentence explanation of the problem were far better than your pseudo code, the sample below is according to the former. There is nothing complicated so I won't explain anything, but ask if you don't get it:
VB.NET:
Expand Collapse Copy
'get file infos
Dim d1 As New IO.DirectoryInfo("folder1")        
Dim d2 As New IO.DirectoryInfo("folder2")
Dim aInfos() As IO.FileInfo = d1.GetFiles
Dim bInfos() As IO.FileInfo = d2.GetFiles
Dim dupNames = From a In aInfos Join b In bInfos On a.Name Equals b.Name Select a.Name

'first ListView
For Each info As IO.FileInfo In aInfos
    Dim lvi As New ListViewItem(info.Name)
    lvi.Name = info.Name
    lvi.SubItems.Add(info.Length.ToString)
    lvi.SubItems.Add(info.LastWriteTime.ToString)
    lvi.Tag = info
    If Not dupNames.Contains(info.Name) Then
        lvi.BackColor = Color.Red
    End If
    Me.ListView1.Items.Add(lvi)
Next

'second ListView
For Each info As IO.FileInfo In bInfos
    Dim lvi As New ListViewItem(info.Name)
    lvi.Name = info.Name
    lvi.SubItems.Add(info.Length.ToString)
    lvi.SubItems.Add(info.LastWriteTime.ToString)
    lvi.Tag = info
    If Not dupNames.Contains(info.Name) Then
        lvi.BackColor = Color.Red
    End If
    Me.ListView2.Items.Add(lvi)
Next

'compare where duplicate file names
For Each dup As String In dupNames
    Dim itemA As ListViewItem = ListView1.Items(dup)
    Dim itemB As ListViewItem = ListView2.Items(dup)
    Dim infoA As IO.FileInfo = CType(itemA.Tag, IO.FileInfo)
    Dim infoB As IO.FileInfo = CType(itemB.Tag, IO.FileInfo)
    If infoA.Length <> infoB.Length OrElse infoA.LastWriteTime <> infoB.LastWriteTime Then
        itemB.BackColor = Color.Yellow
    End If
Next
wjburke2 said:
My main concern is the VB.2008 does not have indexing making it hard to move around
Does it not, really? :p
 
Back
Top