What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleCompare the contents of two directories in Visual Basic .NET
DescriptionThis example shows how to compare the contents of two directories in Visual Basic .NET.
Keywordsdirectory, directory contents, files, compare directories, compare files, Visual Basic .NET, VB.NET
CategoriesFiles and Directories, VB.NET
When you fill in the names of two directories and click the Compare button, the following code executes. It uses My.Computer.FileSystem.GetFiles to get ReadOnlyCollections listing the files in the two directories. The code then calls subroutine SortCollection to sort the collections and turn them into arrays.

Next the code loops through the arrays comparing their entries. If two entries match, the code adds them both to the program's DataGridView control. If the files don't match, the program adds the one that comes alphabetically first to the DataGridView and increments its array's counter.

When the code finishes with all of the files in one of the arrays, it dumpes the remaining items into the DataGridView.

' List the files in each directory.
Private Sub btnCompare_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCompare.Click
    ' Get sorted lists of files in the directories.
    Dim files1 As ReadOnlyCollection(Of String) = _
    Dim file_names1() As String = SortCollection(files1)

    Dim files2 As ReadOnlyCollection(Of String) = _
    Dim file_names2() As String = SortCollection(files2)

    ' Compare.
    Dim i1 As Integer = 0
    Dim i2 As Integer = 0
    Do While (i1 < file_names1.Length) AndAlso (i2 < _
        If file_names1(i1) = file_names2(i2) Then
            ' They match.
            dgvFiles.Rows.Add(New Object() _
                {file_names1(i1), file_names2(i2)})
            i1 += 1
            i2 += 1
        ElseIf file_names1(i1) < file_names2(i2) Then
            ' Display the directory 1 file.
            dgvFiles.Rows.Add(New Object() _
                {file_names1(i1), Nothing})
            i1 += 1
        ElseIf file_names1(i1) > file_names2(i2) Then
            ' Display the directory 2 file.
            dgvFiles.Rows.Add(New Object() {Nothing, _
            i2 += 1
        End If

    ' Display remaining directory 1 files.
    For i As Integer = i1 To file_names1.Length - 1
        dgvFiles.Rows.Add(New Object() {file_names1(i), _
    Next i

    ' Display remaining directory 2 files.
    For i As Integer = i2 To file_names2.Length - 1
        dgvFiles.Rows.Add(New Object() {Nothing, _
    Next i
End Sub
Subroutine SortCollection takes a ReadOnlyCollection(Of String) as a parameter. It builds an array and copies the collection's values into it. It then calls the Quicksort subroutine to sort the array and returns the result.
' Build an array holding the sorted values in
' the ReadOnlyCollection(Of String).
Public Function SortCollection(ByVal coll As _
    ReadOnlyCollection(Of String)) As String()
    ' Move the items into an array.
    Dim result(coll.Count - 1) As String
    For i As Integer = 0 To coll.Count - 1
        ' Get just the file title.
        Dim file_info As New FileInfo(coll(i))
        result(i) = file_info.Name
    Next i

    ' Sort the array.
    Quicksort(result, 0, coll.Count - 1)

    ' return the result.
    Return result
End Function
Download the example to see how the Quicksort subroutine works or look at this Web page.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.