Comparing last write times of files in two different folders

wayliff

Member
Joined
Feb 5, 2007
Messages
10
Programming Experience
Beginner
Hello,

I'm writing code to programatically compare the last write time of files in two directories.

I have written the following to obtain the list of files from the two directories

For Each oldf As String In System.IO.Directory.GetFiles(oldpath)
Dim oldfilename As String = oldf.Substring(oldf.LastIndexOf("\") + 1)
Dim oldfilenametime As DateTime = System.IO.File.GetLastWriteTime(oldf)
'ListBox1.Items.Add(((oldfilename) & " " & (oldfilenametime)).ToString)
Next
For Each newf As String In System.IO.Directory.GetFiles(newpath)
Dim newfilename As String = newf.Substring(newf.LastIndexOf("\") + 1)
Dim newfilenametime As DateTime = System.IO.File.GetLastWriteTime(newf)
'ListBox2.Items.Add(((newfilename) & " " & (newfilenametime)).ToString)
Next

I tested it by adding the results to list boxes and it works....however now I am not sure how to compare the two files time and display something like "file A is newer than file B" or "file A is older than file B".

Can anybody offer some help doing this?

Thanks
 
Thanks for your reply.

Yes, indeed that would help the comparison process however my dilemma is that oldf and newf do not live outside the For Each statements and so these would have to be generated somewhoe...Does this current approach i'm using make sense? or is there an easier way to do it?
 
Yes, indeed that would help the comparison process however my dilemma is that oldf and newf do not live outside the For Each statements

Well, yes.. but it's your fault for Dimming them inside the loop!

"Doctor it hurts when I raise my arm like this"
"So don't raise your arm like that"


More helpfully, you might consider creating a DataTable with a string and a DateTime columns, populate 2 instances of this DataTable with the details. The list's datasources can be set to the tables, and you have thus retained all the detail you want..

VB.NET:
Dim x as MyDataTable, y as MyDataTable
For Each thing in olds
  x.AddRow(oldname, olddate)
NExt
For Each thing in news
  y.AddRow(newname, newtime)
next
list1.DAtaSOurce = x
list1.DisplayMember = "TheNameCOlumn"
...
 
Here's how I did it, using 2 ListView controls with three columns each (name,lastwritedate,comparison).
First the main code, populates both listview and make the comparison:
VB.NET:
folderToListView(lvOld, New IO.DirectoryInfo(oldpath))
folderToListView(lvNew, New IO.DirectoryInfo(newpath))
compareListViews(lvOld, lvNew)
Here the method that populates the Listview control:
VB.NET:
    Sub folderToListView(ByVal lv As ListView, ByVal path As IO.DirectoryInfo)
        lv.Items.Clear()
        lv.Tag = path
        Dim lvi As ListViewItem
        For Each info As IO.FileInfo In path.GetFiles
            lvi = lv.Items.Add(info.Name)
            lvi.Name = info.Name
            lvi.SubItems.Add(info.LastWriteTime)
            lvi.SubItems.Add("")
        Next
    End Sub
Here the method that compares and writes the results to the 3rd column of each file in listview:
VB.NET:
    Sub compareListViews(ByVal lvOld As ListView, ByVal lvNew As ListView)
        Dim lviNew As ListViewItem, dtOld, dtNew As Date
        'first reset one
        For Each lviNew In lvNew.Items
            lviNew.SubItems(2).Text = "unique"
        Next
        'compare
        For Each lviOld As ListViewItem In lvOld.Items
            lviNew = lvNew.Items(lviOld.Text)
            If lviNew Is Nothing Then
                lviOld.SubItems(2).Text = "unique"
            Else
                dtOld = CDate(lviOld.SubItems(1).Text)
                dtNew = CDate(lviNew.SubItems(1).Text)
                If dtOld = dtNew Then
                    lviOld.SubItems(2).Text = "equal"
                    lviNew.SubItems(2).Text = "equal"
                ElseIf dtOld > dtNew Then
                    lviOld.SubItems(2).Text = "newer"
                    lviNew.SubItems(2).Text = "older"
                Else
                    lviOld.SubItems(2).Text = "older"
                    lviNew.SubItems(2).Text = "newer"
                End If
            End If
        Next
    End Sub
This solution is UI based, if you want to see the files and dates and result and make UI choices.

There is a whole lot of different ways of doing this, including not using UI, and storing the info in a Dataset or list of FileInfo or your own business object.
 
Ok using what has been suggested i came up with this, which I think it should work fine however i'm getting white hairs with conversion of string to date which is not valid...when it sets dtold and dtnew

For Each oldf As String In System.IO.Directory.GetFiles(oldpath)
Dim oldfilename As String = oldf.Substring(oldf.LastIndexOf("\") + 1)
Dim oldfilenametime As DateTime = System.IO.File.GetLastWriteTime(oldf)
ListView1.Items.Add(oldfilename)
ListView1.Items.Add(oldfilenametime)
ListView1.Items.Add(
"")
Next
ListView2.Items.Clear()
For Each newf As String In System.IO.Directory.GetFiles(newpath)
Dim newfilename As String = newf.Substring(newf.LastIndexOf("\") + 1)
Dim newfilenametime As DateTime = System.IO.File.GetLastWriteTime(newf)
ListView2.Items.Add(newfilename)
ListView2.Items.Add(newfilenametime)
ListView2.Items.Add(
"")
Next
For Each olddate As ListViewItem In ListView1.Items
dtold =
CDate(ListView1.Items(2).Text)
dtnew =
CDate(ListView2.Items(2).Text)
If dtold = dtnew Then
ListView1.Items(3).Text = ""
ListView2.Items(3).Text = ""
ElseIf dtold > dtnew Then
ListView1.Items(3).Text = "is newer than"
ListView2.Items(3).Text = "is older than"
ElseIf dtold < dtnew Then
ListView1.Items(3).Text = "is older than"
ListView2.Items(3).Text = "is newer than"
End If
 
Back
Top