How to get files in order?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I'm using the following For Each loop to read files. How can I get the
files in either date stamp order or filename order?

For Each nntptobbs As String In Directory.GetFiles(NntpDir, "*.nntptobbs")
 
You should sort the array return by GetFiles. Array.Sort method is what
you need, you might also need to implement a comparer.
 
Terry Olsen said:
I'm using the following For Each loop to read files. How can I get the
files in either date stamp order or filename order?

Quick and dirty:

\\\
Imports System.Collections
Imports System.IO
..
..
..
Dim FileNames() As String = Directory.GetFiles("C:\WINDOWS")
Array.Sort(FileNames, New LastWriteTimeComparer)
Me.ListBox1.DataSource = FileNames
..
..
..
Public Class LastWriteTimeComparer
Implements IComparer

Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compare
Dim d As Long = _
File.GetLastWriteTime(DirectCast(x, String)).Ticks - _
File.GetLastWriteTime(DirectCast(y, String)).Ticks
Select Case d
Case Is > 0
Return 1
Case Is < 0
Return -1
Case Else
Return 0
End Select
End Function
End Class
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top