c# - How to sort and delete specific files? -


(i didn't realize long posts frowned upon)

my program reads directory of files looks similar following:

bkupsalesreportjan2011(txt).zip bkupsalesreportjan2011(pdf).zip bkupsalesreportjan2011(doc).zip bkupsalesreportfeb2011(txt).zip bkupsalesreportmar2011(doc).zip bkupsalesreportmar2011(pdf).zip goes on few hundred more files... 

i want save 1 copy of each report based on file type (in order of priority). want keep pdfs , delete duplicates. if there no pdfs keep docs , lastly keep txts.

what best way implement sorting , deleting using visual c# , windows forms?

you can use regex parse file names data , linq duplicates or distinct records.

poco:

public class filedata {     public string original { get; set; }     public string type { get; set; }     public string name { get; set; }      public int weight { { return getweight(type); } }      private static int getweight(string option)     {         // put files in order pdf, doc, txt, etc         switch(option)         {             case "pdf":                 return 0;             case "doc":                 return 1;             case "txt":                 return 2;             default:                 return 3;         }     } } 

you need weight function since default orderby work alphabetically. way can specify files have more significance.

code:

// can substitute directory.getfiles // e.g. var files = directory.getfiles("path/to/files"); var files = new [] {     "bkupsalesreportjan2011(txt).zip",     "bkupsalesreportjan2011(pdf).zip",     "bkupsalesreportjan2011(doc).zip",     "bkupsalesreportfeb2011(txt).zip",     "bkupsalesreportmar2011(doc).zip",     "bkupsalesreportmar2011(pdf).zip" };  var pattern = @"(?<filename>.+)\((?<filetype>\w+)\)\.zip"; // (?<filename>.+) match first part in named group // \( match first open parenthesis // (?<filetype>\w+) match txt/pdf/doc/whatever in named group // \) match closing parenthesis // \.zip match period followed zip  var matchedfiles = files.select(f => regex.match(f, pattern))                         .where(m => m.success)                         .select(f =>                             new filedata                                 {                                     type = f.groups["filetype"].value,                                     name = f.groups["filename"].value,                                     original = f.value                                 }                                ).tolist();  // group files name e.g. bkupsalesreportjan2011 // transform group order , take first record // take original file name originals var distinct = matchedfiles.groupby(f => f.name)                            .select(g => g.orderby(f => f.weight).first())                            .select(f => f.original);  // group files name e.g. bkupsalesreportjan2011 // transform group order , skip first record // since records still in nested ienumerable need flatten // take original file name duplicates var duplicates = matchedfiles.groupby(f => f.name)                              .select(g => g.orderby(f => f.weight).skip(1))                              .selectmany(g => g)                              .select(f => f.original); 

see also:

directory.getfiles


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -