I have two folders say A and B having exactly same no .of files with same name but different modified date and size. I want to compare each file of A to its respective file of folder B and get the newly modified file in a List box. If newly modified file belongs to folder A then it will come to ListBox1 and if its belongs to Folder B, then comes to listbox2. I have started the code but m struck..!! can anyone of you help me?

Dim PathA As String= "" 'path for folder A 
Dim PathB As String= "" 'path for folder B 
Dim Diir1 As New System.Io.DirectoryInfo(PathA) 
Dim Diir2 As New System.Io.DirectoryInfo(PathB) 
Dim List1 = Dir1.GetFiles(.,System.IO.SearchOption.AllDirectories) 
Dim List2 = Dir2.GetFiles(.,System.IO.SearchOption.AllDirectories) 
Dim myFileCompare As New fileCompare 
Dim areIdentical As Boolean = List1.sequence Equals (List2,myFileCompare)

What to do now???

This shoud do what you want:

Imports System.IO
Public Class Form1
    Dim NewFilecompare As New FileCompare

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetModifiedFiles("C:\Test3\test4", "C:\test3\test5")

    End Sub
    Private Sub GetModifiedFiles(ByVal PathA As String, ByVal PathB As String)
        Dim Dir1 As New System.IO.DirectoryInfo(PathA)
        Dim Dir2 As New System.IO.DirectoryInfo(PathB)
        Dim List1 = Dir1.GetFiles("*.*", SearchOption.AllDirectories)
        Array.Sort(List1, AddressOf SortArray)
        Dim List2 = Dir2.GetFiles("*.*", SearchOption.AllDirectories)
        Array.Sort(List2, AddressOf SortArray)
        For I = 0 To List1.Length - 1
            Dim CompareResult As Integer = NewFilecompare.Compare(List1(I), List2(I))
            If CompareResult = 1 Then
            ElseIf CompareResult = -1 Then
            End If
    End Sub
    Private Function SortArray(ByVal x As FileInfo, ByVal y As FileInfo) As Integer
        Return x.Name.CompareTo(y.Name)
    End Function
End Class

Class FileCompare
    Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo)

    Public Function Compare(ByVal x As System.IO.FileInfo, ByVal y As System.IO.FileInfo) _
        As Integer Implements System.Collections.Generic.IComparer(Of System.IO.FileInfo).Compare
        Return x.LastWriteTimeUtc.CompareTo(y.LastWriteTimeUtc)

    End Function
End Class

This code assumes that like you said both folders contain the exact same number of files and that the files in both folders are named the same. what this does is sort the arrays of fileinfo from each folder by file name. Then it compares the items in each array and compares the lastwritetime. Based on the returned value the name is written to either listbox1 or listbox2 or is ignored.

This article has been dead for over six months. Start a new discussion instead.