Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCompare the contents of two directories in Visual Basic 6
DescriptionThis example shows how to compare the contents of two directories in Visual Basic 6.
Keywordsdirectory, directory contents, files, compare directories, compare files, Visual Basic 6
CategoriesFiles and Directories
 
When you fill in the names of two directories and click the Compare button, the following code executes. It uses the Dir function to get the names of the files in each directory and stores them in arrays. (Note that the file name in the TextBoxes should include anym wildcards. For example, C:\whatever\dir1\*.*.)

The code then calls the Quicksort subroutine to sort the arrays. Download the example to see how the Quicksort subroutine works or look at this Web page.

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.

 
Private Sub cmdCompare_Click()
Dim file_names1() As String
Dim file_names2() As String
Dim num_file_names1 As Integer
Dim num_file_names2 As Integer
Dim file_name As String
Dim i1 As Integer
Dim i2 As Integer
Dim i As Integer

    ' Get the files in the first directory.
    file_name = Dir$(txtDir1.Text)
    num_file_names1 = 0
    Do While Len(file_name) > 0
        num_file_names1 = num_file_names1 + 1
        ReDim Preserve file_names1(1 To num_file_names1)
        file_names1(num_file_names1) = file_name

        file_name = Dir$()
    Loop

    ' Get the files in the second directory.
    file_name = Dir$(txtDir2.Text)
    num_file_names2 = 0
    Do While Len(file_name) > 0
        num_file_names2 = num_file_names2 + 1
        ReDim Preserve file_names2(1 To num_file_names2)
        file_names2(num_file_names2) = file_name

        file_name = Dir$()
    Loop

    ' Sort the lists of files.
    Quicksort file_names1, 1, num_file_names1
    Quicksort file_names2, 1, num_file_names2

    ' Compare.
    lvwFiles.ListItems.Clear
    i1 = 1
    i2 = 1
    Do While (i1 <= num_file_names1) And (i2 <= _
        num_file_names2)
        If file_names1(i1) = file_names2(i2) Then
            ' They match.
            AddFiles file_names1(i1), file_names2(i2)
            i1 = i1 + 1
            i2 = i2 + 1
        ElseIf file_names1(i1) < file_names2(i2) Then
            ' Display the directory 1 file.
            AddFiles file_names1(i1), ""
            i1 = i1 + 1
        ElseIf file_names1(i1) > file_names2(i2) Then
            ' Display the directory 2 file.
            AddFiles "", file_names2(i2)
            i2 = i2 + 1
        End If
    Loop

    ' Display remaining directory 1 files.
    For i = i1 To num_file_names1
        AddFiles file_names1(i), ""
    Next i

    ' Display remaining directory 2 files.
    For i = i2 To num_file_names2
        AddFiles "", file_names2(i)
    Next i
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated