Question Comparing Time Span between files

ddivita

Member
Joined
May 7, 2008
Messages
19
Programming Experience
10+
I want to compare how many seconds there are between files. If the files are
within a 1 - 10 second range I want to copy them to their own folders. What I have so far is a couple methods that take in all the files in a directory /
subdirectories and sort them by their lastWriteTime stamp:
VB.NET:
Dim flist As List(Of System.IO.FileInfo) ' private member

Private Sub writeDirectories(ByVal ParentPath As String)
        For Each sDirectory As String In 
System.IO.Directory.GetDirectories(ParentPath)
           flist.AddRange(New DirectoryInfo(sDirectory).GetFiles())

            writeDirectories(sDirectory)

        Next
    End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles Me.Load
        flist = New List(Of System.IO.FileInfo)
        writeDirectories("C:\Documents and Settings\ddivita\My Documents")
        flist.Sort(AddressOf SortByLastWriteTime)  
End Sub

Private Function SortByLastWriteTime(ByVal x As System.IO.FileInfo, ByVal y 
As System.IO.FileInfo) As Integer
        Return x.LastWriteTime.CompareTo(y.LastWriteTime)
End Function

I then want to take the flist and compare the TimeSpan between files. Here
is an example:

Items in flist:
File1 - TimeStamp - 5:16:46
File2 - TimeStamp - 5:16:50
File3 - TimeStamp - 5:16:54
File4 - TimeStamp - 3:56:37
File5 - TimeStamp - 3:56:42

In the above example files 1 - 3 would go into a folder while 4 & 5 are put in another.

I guess I would need to compare file1 to file2, then file2 to file3, and
so on. I figure I could use the timespan class to compare the seconds. Any ideas? Is there a better way?
Thanks

Daniel
 
Last edited by a moderator:
1.. You can tell GetFile() to work recursively for you.. you didnt have to write your own recursion routine

2.. Youre a good way the way there though already.. Rather than do it for you, i'll write the pseudocode

VB.NET:
'declare a temp variable tempDate to hold the time and set its time to DateTime.MinValue
'declare a temp variable tempInt for an integer, that will e used to uniquely name folder names

'while there are more than 0 items in my list

  'date1 - date2 realises a TimeSpan of the difference between the dates
  'if the TotalSeconds of the (list(0) last write time - tempDate) is > 10 then
    'increment the tempInt
    'update the tempDate to list(0) last write time

  'move the list(0) file into a folder named "myFolder" + tempInt

  'remove the first entry in the list

The logic is simple:
start with a very early date, earlier than possible of a file
each time the file date is more than 10 seconds from that date, consider that files date as a new checkpoint
process all the files in date order, everyone that is within 10 seconds of the checkpoint, gets put in the same named folder
i chose to call my folders myFOlder1 myFolder2 ... you may prefer another naming convention


Note that if you mean that all files dates within 10 seconds of the previous date (which is different from above but your example doesnt allow me to tell what you mean) then yopu must update the checkpoint on every pass of the loop, not jsut when it gets >10 seconds since the last time

for example consider the files:

00:10:00
00:10:01
00:10:09
00:10:11


my logic will put files 1, 2 and 3 in myFolder1, and file 4 in myFolder2
If you mean taht all files dated within 10 seconds of the previous file should go in one folder, all the files would end up in the SAME folder myFolder1
 
Back
Top