static void Main(string[] args) { using (var ctx = new FontInfosEntities()) { Console.WriteLine("Selecting paths"); foreach (var folder in ctx.Folders) { Console.WriteLine(folder.Path); foreach (var font in folder.Fonts) { if (File.Exists(font.FullName)) { continue; } Console.WriteLine($"\t{font.FullName}"); ctx.Fonts.Remove(font); } } if (ctx.ChangeTracker.HasChanges()) { Console.WriteLine("Saving changes"); ctx.SaveChanges(); } } Console.WriteLine("Done."); Console.ReadLine(); }
private static void ProcessFiles(string folder) { // Get the list of files found in the directory. var dir = new DirectoryInfo(folder); var files = dir.GetFiles("*.?tf").ToList(); var fileCount = files.Count; using (var ctx = new FontInfosEntities()) { var FolderInDb = ctx.Folders.FirstOrDefault(f => f.Path == folder) ?? new Folder() { Path = folder }; if (FolderInDb.Fonts.Any()) { var fontsInDb = FolderInDb.Fonts; // remove from DB fonts not in folder var filesHashSet = new HashSet <string>(files.Select(x => x.FullName)); var ObsoleteFonts = fontsInDb.Where(f => !filesHashSet.Contains(f.FullName)).ToList(); if (ObsoleteFonts.Any()) { ctx.Fonts.RemoveRange(ObsoleteFonts); ctx.SaveChanges(); } // do not process fonts already in database var fontsHashSet = new HashSet <string>(fontsInDb.Select(f => f.FullName)); files.RemoveAll(f => fontsHashSet.Contains(f.FullName)); } if (!files.Any()) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine($"\n{folder} --- {fileCount}"); return; } var fonts = files.Select(f => new FontInfo(f, FolderInDb)); try { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"\n{folder} --- {fileCount} --- {fonts.Count()}"); ctx.Fonts.AddRange(fonts); ctx.SaveChanges(); } catch (Exception ex) { _errors.Add(folder); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine(ex.Message); Console.ReadLine(); } } }
private static void RemoveObsoleteFoldersFromDb(string folder) { using (var ctx = new FontInfosEntities()) { var foldersInDb = ctx.Folders.Where(f => f.Path.StartsWith(folder)).ToList(); if (!foldersInDb.Any()) { return; } // Remove empty folders and folders than no longer exist var obsoleteFolders = foldersInDb .Where(f => !f.Fonts.Any() || !Directory.Exists(f.Path)).ToList(); ctx.Folders.RemoveRange(obsoleteFolders); ctx.SaveChanges(); } }
static void Main(string[] args) { var Errors = new List <string>(); using (var ctx = new FontInfosEntities()) { foreach (var dup in ctx.Duplicates.Where( d => d.Folder.StartsWith(@"D:\Fonts1\Fonts Categorized\"))) { Console.WriteLine(dup.Folder + dup.FileName); try { //File.Delete(dup.Folder + dup.FileName); } catch { Errors.Add(dup.FileName); } } } Console.WriteLine("Done"); Console.ReadLine(); }
private void Form1_Load(object sender, EventArgs e) { _ctx = new FontInfosEntities(); }