Help with Date comparison method
Details:
- Unknown # of folder in directory C:\...\releases\?
- Get the creation dates of those folders.
- Compare an unknown quantity of folders of their date of creation property.
- Retreive the folder's name from the list that has the creation date.
Code:
Private Sub GetLatestFolder( )
Dim createDates As New List(Of Date) 'Folder's corresponding date values
Dim Folders As New List(Of String) 'Name of Folders themselves
Dim counter As Integer = 0
Dim result As Integer = 0 'Result from comparing dates
Dim version As String 'The unknown version # of the latest folder
Try
Dim myFolders As IEnumerable(Of DirectoryInfo) = Directory.GetDirectories("C:\Sound Manager\Design\projects\\releases", "*", SearchOption.TopDirectoryOnly).Select(Function(x) New DirectoryInfo(x))
For Each fName In myFolders
createDates.Add(fName.CreationTime())
Folders(counter) = fName.Name()
counter = counter + 1
Next
If (Folders.Count >= 2) Then
For i As Integer = 0 To Folders.Count - 1
result = Date.Compare(createDates(i), createDates(i + 1))
Next
End If
If result < 0 Then
version = Folders(1)
Else
version = Folders(0)
End If
Return version
End sub
Re: Help with Date comparison method
Code:
'get folder names + sort by creation datetime
Dim Folders As List(Of String) = IO.Directory.GetDirectories("C:\Sound Manager\Design\projects\\releases", "*", IO.SearchOption.TopDirectoryOnly).OrderBy(Function(f) New IO.FileInfo(f).CreationTime).ToList
'get corresponding creation datetimes
Dim createDates As List(Of Date) = Folders.ConvertAll(Function(f) New IO.FileInfo(f).CreationTime)
Re: Help with Date comparison method
Quote:
Compare an unknown quantity of folders of their date of creation property.
Retreive the folder's name from the list that has the creation date.
That doesn't make sense. I assume you mean to sort the folders by their date of creation but what name are you wanting to retrieve? The oldest, the newest, the one that was created nearest to last Tuesday?
Re: Help with Date comparison method
Quote:
Originally Posted by
dunfiddlin
That doesn't make sense. I assume you mean to sort the folders by their date of creation but what name are you wanting to retrieve? The oldest, the newest, the one that was created nearest to last Tuesday?
The newest one.
Re: Help with Date comparison method
using my code from post #2, the newest 1 will be Folders.Last