Last active
August 29, 2015 14:18
-
-
Save cskiwi/d2e5002af98da6982971 to your computer and use it in GitHub Desktop.
Comparing folders testing (working)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CheckFolder { | |
class Program { | |
public static List<Folder> toAddList; | |
static void Main(string[] args) { | |
var newFolders = new List<Folder>(); | |
Folder mainFolder, otherFolder; | |
var r = new Random(); | |
setup(r, "sub 0", out mainFolder, subLevels: 20, maxFolders: 20); // simulates existing folder | |
setup(r, "sub 0", out otherFolder, subLevels: 20, maxFolders: 20); // simulates drop folder | |
Console.WriteLine(" --- Folders ---"); | |
printFolder(mainFolder); | |
Console.WriteLine(""); | |
printFolder(otherFolder); | |
Console.WriteLine(""); | |
Console.WriteLine(" --- Comparing ---"); | |
compareFolders(mainFolder, otherFolder, newFolders); | |
Console.WriteLine(""); | |
Console.WriteLine(" --- New Folders ---"); | |
printNewFolders(newFolders); | |
#if DEBUG | |
Console.WriteLine("Press enter to close..."); | |
Console.ReadLine(); | |
#endif | |
} | |
private static void printNewFolders(List<Folder> newFolders) { | |
newFolders.ForEach(i => Console.WriteLine("Create folder:" + i.title)); | |
} | |
private static void compareFolders(Folder folder1, Folder folder2, List<Folder> newFolders) { | |
if (folder2.childs != null) { | |
if (folder1.childs != null) { | |
// check for folder 2 childs not in folder 1 | |
newFolders.AddRange(folder2.childs.FindAll(f => !folder1.childs.Select(f2 => f2.title).ToList().Contains(f.title))); | |
// for similar folders go one deeper | |
folder1.childs.FindAll(f => folder2.childs.Select(f2 => f2.title).ToList().Contains(f.title)).ForEach(f1 => compareFolders(f1, folder2.childs.Find(f2 => f2.title == f1.title), newFolders)); | |
} else { | |
// folder 1 doesn't have childs so add all of folder2 | |
newFolders.AddRange(folder2.childs); | |
} | |
} | |
} | |
private static void printFolder(Folder node, bool printfiles = false, int index = 0) { | |
// Print folder name | |
for (int i = 0; i < index; i++) Console.Write(" > "); | |
Console.WriteLine(node.ToString()); | |
// Print files | |
if (node.files != null && node.files.Count > 0 && printfiles) { | |
foreach (File file in node.files) { | |
for (int i = 0; i < index; i++) Console.Write(" > "); | |
Console.WriteLine(" > " + file.ToString()); | |
} | |
} | |
// Print childs if | |
if (node.childs != null) { | |
index++; | |
foreach (Folder sub in node.childs) { | |
printFolder(sub, printfiles, index); | |
} | |
} | |
} | |
#region setup | |
private static void addFiles(Folder folder, int amount) { | |
for (int i = 0; i < amount; i++) { | |
folder.addFile(new File("Test File " + i)); | |
} | |
} | |
private static void setup(Random r, string title, out Folder root, int subLevels, int maxFolders = 5) { | |
root = new Folder(title); | |
var rootSubFolder = 2; | |
for (int i = 0; i < rootSubFolder; i++) { | |
var sub = new Folder(root.title + " sub " + i); | |
createFolders(r, sub, subLevels, maxFolders: maxFolders); | |
root.addSubFolder(sub); | |
} | |
} | |
private static void createFolders(Random r, Folder folder, int maxindex = 1, int index = 0, int maxFolders = 5) { | |
index++; | |
for (int i = 0; i < r.Next() % maxFolders-1; i++) { | |
// make new sub folder | |
var title = ""; | |
title += folder.title + " "; | |
title += "sub " + i; | |
var fol = new Folder(title); | |
// add some files | |
addFiles(fol, r.Next() % 5); | |
// append to root | |
folder.addSubFolder(fol); | |
} | |
if (index < maxindex - 1 && folder.childs != null && folder.childs.Count > 0) { | |
foreach (Folder sub in folder.childs) { | |
createFolders(r, sub, maxindex, index); | |
} | |
} | |
} | |
#endregion | |
} | |
#region classes | |
public class Folder : document { | |
public List<Folder> childs; | |
public List<File> files; | |
public Folder(string title) : | |
base(title) { | |
} | |
public void addSubFolder(Folder child) { | |
if (childs == null) childs = new List<Folder>(); | |
childs.Add(child); | |
child.root = this; | |
} | |
public void addFile(File file) { | |
if (files == null) files = new List<File>(); | |
files.Add(file); | |
file.root = this; | |
} | |
public string ToString() { | |
string output = "Fo: " + title; | |
if (childs != null && childs.Count > 0 || files != null && files.Count > 0) { | |
output += " ("; | |
if (childs != null && childs.Count > 0) output += "fo: " + childs.Count; | |
if (childs != null && childs.Count > 0 && files != null && files.Count > 0) output += ", "; | |
if (files != null && files.Count > 0) output += "fi: " + files.Count; | |
output += ")"; | |
} | |
return output; | |
} | |
} | |
public class File : document { | |
public File(string title) : | |
base(title) { | |
} | |
public string ToString() { | |
string output = "Fi: " + title; | |
return output; | |
} | |
} | |
public class document { | |
public string title; | |
public Folder root; | |
public document(string title) { | |
this.title = title; | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment