Shared functions to sort a directories files by last write time, last access time or created time

Posted by Blake on 12/26/2011
)

Here are a few shared functions I put together to sort files in a directory by their date attributes.

Imports System.IO
''' <summary>
''' blakepell@hotmail.com
''' </summary>
''' <remarks></remarks>
Public Class FileSystemUtilities
    Public Enum SortOrder
        Ascending
        Decending
    End Enum
    Public Enum SortType
        LastWriteTime
        LastAccessTime
        CreationTime
    End Enum
    ''' <summary>
    ''' Gets all files in a directory and orders them in ascending or decending order by modified date or
    ''' created date.
    ''' </summary>
    ''' <param name="dir">The directory to return files for.</param>
    ''' <param name="st">Which attribute the directory should be sorted by.</param>
    ''' <param name="so">The sort order.  Decending will sort newest to oldest.  Ascending will sort oldest to newest.</param>
    ''' <returns>A generic string list.</returns>
    ''' <remarks></remarks>
    Public Shared Function GetFilesByModifiedDate(ByVal dir As String, ByVal st As SortType, ByVal so As SortOrder) As List(Of String)
        dir = dir.Trim("")
        Dim di As DirectoryInfo = New DirectoryInfo(dir)
        Dim files As FileSystemInfo() = di.GetFileSystemInfos()
        Dim orderedFiles
        Dim returnList As New List(Of String)
        Select Case st
            Case SortType.LastWriteTime
                If so = SortOrder.Decending Then
                    orderedFiles = files.OrderBy(Function(f) f.LastWriteTime).Reverse
                Else
                    orderedFiles = files.OrderBy(Function(f) f.LastWriteTime)
                End If
            Case SortType.LastAccessTime
                If so = SortOrder.Decending Then
                    orderedFiles = files.OrderBy(Function(f) f.LastAccessTime).Reverse
                Else
                    orderedFiles = files.OrderBy(Function(f) f.LastAccessTime)
                End If
            Case SortType.CreationTime
                If so = SortOrder.Decending Then
                    orderedFiles = files.OrderBy(Function(f) f.CreationTime).Reverse
                Else
                    orderedFiles = files.OrderBy(Function(f) f.CreationTime)
                End If
            Case Else
                orderedFiles = files.OrderBy(Function(f) f.LastWriteTime).Reverse
        End Select
        For Each fi As FileInfo In orderedFiles
            returnList.Add(fi.Name)
        Next
        Return returnList
    End Function
    ''' <summary>
    ''' Gets all files in a directory and orders them in ascending or decending order by modified date or
    ''' created date.
    ''' </summary>
    ''' <param name="dir">The directory to return files for.</param>
    ''' <param name="st">Which attribute the directory should be sorted by.</param>
    ''' <param name="so">The sort order.  Decending will sort newest to oldest.  Ascending will sort oldest to newest.</param>
    ''' <returns>A generic string list.</returns>
    ''' <remarks></remarks>
    Public Shared Function GetFilesSystemInfosByModifiedDate(ByVal dir As String, ByVal st As SortType, ByVal so As SortOrder) As FileSystemInfo()
        dir = dir.Trim("")
        Dim di As DirectoryInfo = New DirectoryInfo(dir)
        Dim files As FileSystemInfo() = di.GetFileSystemInfos()
        Dim orderedFiles

        Select Case st
            Case SortType.LastWriteTime
                If so = SortOrder.Decending Then
                    orderedFiles = files.OrderBy(Function(f) f.LastWriteTime).Reverse
                Else
                    orderedFiles = files.OrderBy(Function(f) f.LastWriteTime)
                End If
            Case SortType.LastAccessTime
                If so = SortOrder.Decending Then
                    orderedFiles = files.OrderBy(Function(f) f.LastAccessTime).Reverse
                Else
                    orderedFiles = files.OrderBy(Function(f) f.LastAccessTime)
                End If
            Case SortType.CreationTime
                If so = SortOrder.Decending Then
                    orderedFiles = files.OrderBy(Function(f) f.CreationTime).Reverse
                Else
                    orderedFiles = files.OrderBy(Function(f) f.CreationTime)
                End If
            Case Else
                orderedFiles = files.OrderBy(Function(f) f.LastWriteTime).Reverse
        End Select

        Return orderedFiles
    End Function
End Class