Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently working on a small project. I am trying to compare two directories for the folders and the files. For example, if i have a folders named toto in my c:\ and a other one named toto in my e:\ it will compare the files inside. If inside i have toto.txt, toto1.txt, toto2.txt and in the other folders i have toto.txt and toto1.txt, toto232.txt... it will add toto.txt and toto1.txt in my listview! :) but im using the recursive procedures : Recursive Procedures (Visual Basic)[^] and i have some trouble with it! heres is my complete function :

VB
Private Sub comparerfichier(ByVal dir As String, ByVal dir2 As String)

    Try
        Dim directoire1 As New DirectoryInfo(dir)
        Dim directoire2 As New DirectoryInfo(dir2)
        Dim fichierinfo1() As FileInfo = directoire1.GetFiles
        Dim fichierinfo2() As FileInfo = directoire2.GetFiles

        Dim dupNames = From a In fichierinfo1 Join b In fichierinfo2 On a.Name Equals b.Name Select a.Name
        ' info for the listview
        With Me.ListView1
            .View = View.Details
            .GridLines = True
            .FullRowSelect = True
            .CheckBoxes = True
            .Columns.Add("Fichiers", 150, HorizontalAlignment.Left)
            .Columns.Add("Dernière Modification", 120, HorizontalAlignment.Left)
        End With
        ' info sur l autre listview
        With Me.ListView2
            .View = View.Details
            .GridLines = True
            .FullRowSelect = True
            .CheckBoxes = True
            .Columns.Add("Fichiers", 150, HorizontalAlignment.Left)
            .Columns.Add("Dernière Modification", 120, HorizontalAlignment.Left)
        End With

        'add the same files in the listview

        For Each repertoire As FileInfo In fichierinfo1
            'check if the files is the same name
            For Each repertoire2 As FileInfo In fichierinfo2
                If repertoire.Name = repertoire2.Name Then
                    Dim fichier As New ListViewItem(repertoire.Name)
                    Dim fichier2 As New ListViewItem(repertoire2.Name)
                    'files1
                    With fichier
                        .Name = repertoire.Name
                        .SubItems.Add(repertoire.LastWriteTime.ToString)
                        .Tag = repertoire
                    End With
                    'files1
                    With fichier2
                        .Name = repertoire2.Name
                        .SubItems.Add(repertoire2.LastWriteTime.ToString)
                        .Tag = repertoire2
                    End With
                    'add both files
                    Me.ListView1.Items.Add(fichier)
                    Me.ListView2.Items.Add(fichier2)

                End If

            Next
        Next
        ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '                           ERROR IN THIS PART with THE RECURSIVE PROCEDURE
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim directoiresinfo() As DirectoryInfo = directoire1.GetDirectories
        Dim directoires2info() As DirectoryInfo = directoire2.GetDirectories
        Dim directoireGrosseur1() As String = IO.Directory.GetDirectories(dir)
        Dim directoireGrosseur2() As String = IO.Directory.GetDirectories(dir2)

        If directoireGrosseur1.Length <> 0 And directoireGrosseur2.Length <> 0 Then
            For Each info As IO.DirectoryInfo In directoiresinfo
                For Each info2 As IO.DirectoryInfo In directoires2info
                    If info.Name = info2.Name Then
                        Dim dossier As New ListViewItem(info.Name)
                        Dim dossier2 As New ListViewItem(info2.Name)
                        With dossier
                            .Name = info.Name
                            .SubItems.Add(info.LastWriteTime.ToString)
                            .Tag = info
                        End With
                        With dossier2
                            .Name = info2.Name
                            .SubItems.Add(info2.LastWriteTime.ToString)
                            .Tag = info2
                        End With
                        Me.ListView1.Items.Add(dossier)
                        Me.ListView2.Items.Add(dossier2)
                        comparerfichier(info.GetDirectories.ToString, info2.GetDirectories.ToString)
                    End If
                Next
            Next
        End If
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '                                             ERROR IN THIS PART
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        For Each pareil As String In dupNames
            Dim itemA As ListViewItem = ListView1.Items(pareil)
            Dim itemB As ListViewItem = ListView2.Items(pareil)
            Dim infoA As FileInfo = CType(itemA.Tag, FileInfo)
            Dim infoB As FileInfo = CType(itemB.Tag, FileInfo)

            If infoA.Length <> infoB.Length OrElse infoA.LastWriteTime <> infoB.LastWriteTime Then
                itemB.ForeColor = Color.Red
                itemA.ForeColor = Color.Red
            End If
        Next

    Catch ex As Exception
        MessageBox.Show(ex.Message, "Comparer 2 répertoires", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub


What I have tried:

so yeah, my recursive procedures doesn't work great! I'm trying to debug it but I'm not found anything yet! If you guys can help me it would be very appreciated!
Posted
Updated 23-Sep-16 0:53am
v3
Comments
[no name] 22-Sep-16 19:57pm    
"ERROR IN THIS PART" tells us nothing at all about your problem.
Wendelius 22-Sep-16 23:05pm    
What is the detailed problem? If you get an error message, post the complete message or post the complete problem description.

1 solution

I don't understand why you are using a recursive procedure at all. The .GetFiles method has a recursive feature so that it will get all of the files from the sub-folders too.

Your whole method of adding the listitems to each listview seems a little overcomplicated too.

Here is an example of listing all of the files in two folders, including any sub-folders. Duplicate filenames are highlighted in blue and where the duplicated files differ in size or date last updated then they are highlighted in red.

You should be able to adapt this to your specific needs.

VB
Private Sub comparerfichier(ByVal dir As String, ByVal dir2 As String)

    Try
        Dim directoire1 As New DirectoryInfo(dir)
        Dim directoire2 As New DirectoryInfo(dir2)
        Dim fichierinfo1() As FileInfo = directoire1.GetFiles("*.*", SearchOption.AllDirectories)
        Dim fichierinfo2() As FileInfo = directoire2.GetFiles("*.*", SearchOption.AllDirectories)

        'les fichiers en double
        Dim dupNames = From a In fichierinfo1 Join b In fichierinfo2 On a.Name Equals b.Name Select a.Name

        ' info for the listview
        SetUpListView(ListView1)
        ' info sur l autre listview
        SetUpListView(ListView2)

        'add the files to each listview
        FillListView(fichierinfo1, ListView1)
        FillListView(fichierinfo2, ListView2)

        'highlight the duplicates
        For Each pareil As String In dupNames
            Dim itemA As ListViewItem = ListView1.Items(pareil)
            Dim itemB As ListViewItem = ListView2.Items(pareil)
            Dim infoA As FileInfo = CType(itemA.Tag, FileInfo)
            Dim infoB As FileInfo = CType(itemB.Tag, FileInfo)

            If infoA.Length <> infoB.Length OrElse infoA.LastWriteTime <> infoB.LastWriteTime Then
                itemB.ForeColor = Color.Red
                itemA.ForeColor = Color.Red
            Else
                itemA.ForeColor = Color.Blue
                itemB.ForeColor = Color.Blue
            End If
        Next

    Catch ex As Exception
        MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub
Private Sub SetUpListView(lv As ListView)
    With lv
        .View = View.Details
        .GridLines = True
        .FullRowSelect = True
        .CheckBoxes = True
        .Columns.Add("Fichiers", 150, HorizontalAlignment.Left)
        .Columns.Add("Dossier", 150, HorizontalAlignment.Left)
        .Columns.Add("Dernière Modification", 120, HorizontalAlignment.Left)
    End With
End Sub

Private Sub FillListView(fichierinfo() As FileInfo, lv As ListView)
    For Each repertoire As FileInfo In fichierinfo
        Dim fichier As New ListViewItem(repertoire.Name)
        With fichier
            .Name = repertoire.Name
            .SubItems.Add(repertoire.DirectoryName)
            .SubItems.Add(repertoire.LastWriteTime.ToString)
            .Tag = repertoire
        End With
        lv.Items.Add(fichier)
    Next
End Sub
 
Share this answer
 
Comments
Member 12754823 23-Sep-16 16:05pm    
Thanks it works! you're awesome!
CHill60 23-Sep-16 19:10pm    
Thanks .. more inquisitive than awesome I think ... explore those options that come up with intellisense ... I've learned so much that way myself :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900