/ Published in: C#

Also extended C# LINQ and Extension method (simple) to include the last (MAX) Date Created property, added a new Folder class (model) with properties and included them in a List. Also, amended the LINQ statement to get the MAX(Date Created) .
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace CentreFolderDeduper { class Program { static void Main(string[] args) { String[] centreFolders = Directory.GetDirectories(args[0]); foreach(var centreFolder in centreFolders) { if (centreFolder.IndexOf('-') != -1) { ShortName = centreFolder.Left(centreFolder.IndexOf('-')), DateCreated = System.IO.File.GetCreationTime(centreFolder)}; folders.Add(newFolder); } } var duplicateFolders = from f in folders group f by f.ShortName into g where g.Count() > 1 CentreFolderNameCount = g.Count(), LastDateCreated = g.Max(f => f.DateCreated) }; Console.WriteLine(duplicateFolders.Count()); ObjectDumper.Write(duplicateFolders); Console.ReadKey(); } } } class Folder { public String Directoty { get; set; } public String ShortName { get; set; } public DateTime DateCreated { get; set; } } public static string Left(this string str, int count) { if (string.IsNullOrEmpty(str) || count < 1) return string.Empty; else return str.Substring(0, Math.Min(count, str.Length)); }
Comments
