Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:06:52+00:00 2026-06-13T23:06:52+00:00

I have the following code Public Function ListDirLatest(ByVal Dir As String, ByVal Amount As

  • 0

I have the following code

 Public Function ListDirLatest(ByVal Dir As String, ByVal Amount As Integer) As DataTable        
    Dim dt As DataTable = ListDir(Dir)
    If (dt Is Nothing) Then
        Return Nothing
    Else
        Return dt ' This is where i would like to implement the latest x-files logic
    End If
End Function

Private Function ListDir(ByVal Dir As String) As DataTable
    If Directory.Exists(Dir) Then
        Dim dt As DataTable = GetDT()
        Dim dirinfo As New DirectoryInfo(Dir)
        For Each fsi As FileSystemInfo In dirinfo.GetFileSystemInfos(".txt")
            Dim dr As DataRow = dt.NewRow()
            dr("FileName") = fsi.Name()
            dr("FileDate") = fsi.CreationTime()
        Next
        Return dt
    Else
        Return Nothing
    End If
End Function

Private Function GetDT() As DataTable
    'Create DataTable to hold results
    Dim dt As New DataTable("DirList")
    Dim st As System.Type = System.Type.GetType("System.String")
    dt.Columns.Add("FileName", st)
    dt.Columns.Add("FileDate", st)
    Return dt
End Function

At the moment the ListDirLatest Function will return all the files the in the directory.
How do I change the code so that it only returns the latest ‘x’ files, as specified by the Amount argument.

To Clarify I want to return the LATEST ‘x’ files in the directory.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:06:53+00:00Added an answer on June 13, 2026 at 11:06 pm

    You can solve your problem with a little of Linq and a the reference to System.Data.DataSetExtensions
    ( http://msdn.microsoft.com/en-us/library/system.data.datatableextensions(v=vs.100).aspx )

    Public Function ListDirLatest(ByVal Dir As String, ByVal Amount As Integer) As DataTable        
        Dim dt As DataTable = ListDir(Dir)
        If (dt Is Nothing) Then
            Return Nothing
        Else
            Dim r = from myRow in dt.AsEnumerable()
                    Order By("FileDate DESC")
                    Take(Amount)
            dt = r.CopyToDataTable()
            return dt
        End If
    End Function
    

    Also, the ListDir function has a couple of errors

    • Add the row information to the DataTable returned
    • Use a correct pattern for GetFileSystemInfos

      Function ListDir(ByVal Dir As String) As DataTable
          If Directory.Exists(Dir) Then
              Dim dt As DataTable = GetDT()
              Dim dirinfo As New DirectoryInfo(Dir)
              For Each fsi As FileSystemInfo In dirinfo.GetFileSystemInfos("*.txt")
                  Dim dr As DataRow = dt.NewRow()
                  dr("FileName") = fsi.Name()
                  dr("FileDate") = fsi.CreationTime()
                  dt.Rows.Add(dr)
              Next
              Return dt
          Else
              Return Nothing
          End If
      End Function
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Public Shared Function GetListAsString(ByVal data As List(Of String)) As
I have the following code sample in VB .net 2008 Public Function CheckPathFunction(ByVal path
I have a following code which works fine MsgBox(AddSomething(Of String)(Hello, World)) Public Function AddSomething(Of
So let's say I have the following code: public function first(text:String):String { _text =
I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
I have the following piece of code in my Model: public function getSite() {
I have following code public class TEST { public static void main(String arg[]){ try
i have the following code: public static void Serialize() { List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
I have the following code: public class AppDomainArgs : MarshalByRefObject { public string myString;
All, I have the following code: public function addElements() { $newArray = array(); for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.