Results 1 to 3 of 3

Thread: [VB.net]Get Files Based On Creation Date(Most recent created files)

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jun 2014
    Posts
    1

    [VB.net]Get Files Based On Creation Date(Most recent created files)

    I am trying to get a way to list all the files in a directory via datewise in descending order(Example 40 files) i.e most recent created 40 files only. I have searched throughout the internet, but cannot get a right way to do this. Here is what i tried

    Code:
    Dim files() As String = System.IO.Directory.GetFiles(Dir)
    
                Dim fileComparer As IComparer = New CompareFileByDate()
                Array.Sort(files, fileComparer)
                Array.Reverse(files)
    
                For Each f As String In files
                   
                    d.BeginInvoke(f, Dir.Replace(scandir, ""), Nothing, Nothing)
                 
                Next
    
    
    Public Class CompareFileByDate
        Implements IComparer
    
        Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements System.Collections.IComparer.Compare
    
            Dim fia As FileInfo = New FileInfo(a.ToString)
            Dim fib As FileInfo = New FileInfo(b.ToString)
    
            Dim cta As DateTime = fia.LastWriteTime
            Dim ctb As DateTime = fib.LastWriteTime
    
            Return DateTime.Compare(cta, ctb)
        End Function
    
    End Class
    Above code didnt worked.

    Second i tried this one also

    Code:
    Dim files() As IO.FileInfo = New IO.DirectoryInfo(Dir).GetFiles("*.txt").OrderByDescending(Function(fi) fi.LastWriteTime).ToArray
    Didnt worked as well.

    How can i acheive the target ?

    Thanks

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: [VB.net]Get Files Based On Creation Date(Most recent created files)

    Hi,

    You nearly got there with the second example. Here is a complete example for you:-

    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.   Dim myDirectory As New IO.DirectoryInfo("d:\temp")
    3.   Dim myfiles() As String = myDirectory.GetFiles.OrderByDescending(Function(x) x.LastWriteTime).Select(Function(x) x.FullName).Take(40).ToArray
    4.  
    5.   MsgBox(String.Join(Environment.NewLine, myfiles))
    6. End Sub

    Hope that helps.

    Cheers,

    Ian

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [VB.net]Get Files Based On Creation Date(Most recent created files)

    This test gets the 5 most newly created files from the desktop.

    Code:
            Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            Dim di As IO.DirectoryInfo = New IO.DirectoryInfo(path)
    
            'get 5 files in directory OrderByDescending by CreationTime
            Dim foo As List(Of IO.FileInfo) = di.GetFiles("*.*").OrderByDescending(Function(fc) fc.CreationTime).Take(5).ToList
    
            'show results
            For Each lfi As IO.FileInfo In foo
                Debug.WriteLine(lfi.Name & " " & lfi.CreationTime.ToShortDateString & " " & lfi.CreationTime.ToShortTimeString)
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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