Vb.net LINQ from a parent DirectoryInfo loop thru all sub DirectoryInfos Ordered By Modification Date

Dave Owens 1 Reputation point
2022-12-23T19:29:12.787+00:00

Over 2+ years, under File Explorer, I have kept 2 Libraries containing 12 folders which together are populated with more subfolders and more web shortcut links and MsWord docx files. With the passage of months, the organizational value of the subfolders has grown obsolete.

To clean things out, I want to first focus on the Library-child subfolders, to gather and sort them by created- and modification-date. In Vb.Net, Using the DirectoryInfo class via LINQ's OrderBy by Modified Date, I want to create a text file list of all the sub DirectoryInfos and gradually clean them out manually from the oldest to the newest. To create that file, while there are a wealth of GetFiles and C# examples, I do NOT want files or C#. I want directories.

In Vb and LINQ, can you please show me working code that take the Library ShellFiles and creates textfile.WriteString() of all the sorted subdirectory names.

I would be grateful.

Dave

Developer technologies VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2022-12-23T20:48:08.093+00:00

    Hi
    Here is my attempt to provide a text file containing a list of FullName, CreationTime and LastWriteTime sorted on LastWriteTime from a chosen Directory starting from a chosen Date. I am not very adept in using LINQ, as will probably be obvious, but it seems to work. There may be other better solutions suggested.

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Dim savePath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Test1.txt")  
        If Not IO.File.Exists(savePath) Then IO.File.Create(savePath).Close()  
        Dim dirPath As New IO.DirectoryInfo("C:\Users\lesha\Documents\Projects")  
        Dim FromDate As Date = Now.AddMonths(-4).Date  
      
        Dim dirs = From dir In dirPath.EnumerateDirectories() Where dir.CreationTime > FromDate Order By dir.LastWriteTime Select dir.FullName, dir.CreationTime, dir.LastWriteTime  
      
        Using sw As New IO.StreamWriter(savePath, False)  
          sw.WriteLine("FullName,    Creation Time,    LastWrite Time")  
          For i As Integer = 0 To dirs.Count - 1  
            With dirs(i)  
              sw.Write(.FullName)  
              sw.Write(.CreationTime.ToString("dd MMM yyyy"))  
              sw.WriteLine(.LastWriteTime.ToString("dd MMM yyyy"))  
            End With  
          Next  
        End Using  
      End Sub  
    End Class  
      
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.