Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting newest Excel filename from folder
Message
From
25/01/2007 16:05:30
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB.NET 1.1
Miscellaneous
Thread ID:
01187847
Message ID:
01189390
Views:
13
        Private Shared Function GetLatestExcelFileInDir(ByVal dir As String) As String

            Dim dirInfo As System.IO.DirectoryInfo
            Dim files As System.IO.FileInfo()
            Dim datesAndNames As String()
            Dim dt As DateTime

            Try
                'Get directory info of dir
                dirInfo = New System.IO.DirectoryInfo(dir)
                'Get excel files in dir
                files = dirInfo.GetFiles("*.xls")
                'Create string array to hold file names and dates
                datesAndNames = New String(files.Length - 1) {}

                'Enumerate through files
                For i As Integer = 0 To files.Length - 1
                    'Get creation date of file
                    dt = files(i).CreationTime
                    'Store creation date and file name in one text field
                    datesAndNames(i) = dt.ToString("yyyy/MM/dd HH:mm ") & files(i).FullName
                Next

                'Sort the array (ends up sorting by date)
                Array.Sort(datesAndNames)

                'Return file name with date trimmed off
                Return datesAndNames(files.Length - 1).Substring(17)

            Catch ex As Exception
                Throw (New Exception)
            End Try
        End Function
Very fitting: http://xkcd.com/386/
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform