Displaying a list of files by modified date.

Posted on 2007-10-04
Medium Priority
Last Modified: 2013-11-26
I have the below code. I would like to have it not only display the file name and directory, but also the modified date. Each separated as below.

Filename vbtab directory vbtab modifieddate vbcrlf

Sub DirAndFilesViewer(ByVal dir As String, ByVal showFiles As Boolean)
        Dim subdir As String
        Dim fileNm As String
        Dim File As String
       If showFiles Then
            For Each fileNm In Directory.GetFiles(dir)
                File = File & (New String(fileNm & vbTab & vbCrLf))
        End If
' The funciton is called recuresively for each sub directory
For Each subdir In Directory.GetDirectories(dir)
            Me.DirAndFilesViewer(subdir, showFiles)
End Sub
Question by:-cr-
  • 2
LVL 21

Expert Comment

ID: 20018679
My suggestion -> Why not display the Directory.GetFiles and Directory.GetDirectories in a data grid, make all other information hidden. By default the grid would provide an option to sort.
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 20019167
An example of using a DataGridView:
(you can click the column names to sort ascending/descending)

Public Class Form1

    Private dt As New DataTable()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dt.Columns.Add("FileName", GetType(String))
        dt.Columns.Add("Directory", GetType(String))
        dt.Columns.Add("Modified Data", GetType(DateTime))
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DirAndFilesViewer(New System.IO.DirectoryInfo("c:\aaa"))
        DataGridView1.DataSource = dt
    End Sub

    Sub DirAndFilesViewer(ByVal di As System.IO.DirectoryInfo)
        For Each fi As System.IO.FileInfo In di.GetFiles
            dt.Rows.Add(New Object() {fi.Name, fi.DirectoryName, fi.LastWriteTime})
        For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
    End Sub

End Class

Author Comment

ID: 20023670
Idle Mind,
I like it! I have gotten it working for the most part. What I need is to verify that it only displays items within the last week or last day depending on the radio button selected. How would I accomplish that?

If LastWeek.Checked Then
                Dim strTimeFrame As Integer = -7
            End If
            If LastDay.Checked Then
                Dim strTimeFrame As Integer = -1
            End If

If DateDiff(DateInterval.Day, fi.LastWriteTime, Now) = strTimeFrame Then
                dt.Rows.Add(New Object() {fi.Name, fi.DirectoryName, fi.LastWriteTime})
 End If

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20023874
Something like...

    Dim targetDate As Date
    If LastWeek.Checked Then
        targetDate = Date.Today.AddDays(-7)
    End If
    If LastDay.Checked Then
        targetDate = Date.Today.AddDays(-1)
    End If


        If fi.LastWriteTime >= DateTime.Now Then
            dt.Rows.Add(New Object() {fi.Name, fi.DirectoryName, fi.LastWriteTime})
        End If

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question