Results 1 to 5 of 5

Thread: Help with Date comparison method

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    28

    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
    Last edited by Anthony123; Apr 23rd, 2013 at 07:28 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    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)

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help with Date comparison method

    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?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2012
    Posts
    28

    Re: Help with Date comparison method

    Quote Originally Posted by dunfiddlin View Post
    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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with Date comparison method

    using my code from post #2, the newest 1 will be Folders.Last

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width