From 40462412b96b12c72ca6e424fc30a4f4d89e821a Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 28 Oct 2017 16:20:17 +0100 Subject: [PATCH] Initial commit. --- .gitignore | 40 ++++ TreeCompare.sln | 17 ++ TreeCompare/Program.cs | 254 +++++++++++++++++++++++++ TreeCompare/Properties/AssemblyInfo.cs | 26 +++ TreeCompare/TreeCompare.csproj | 39 ++++ 5 files changed, 376 insertions(+) create mode 100644 .gitignore create mode 100644 TreeCompare.sln create mode 100644 TreeCompare/Program.cs create mode 100644 TreeCompare/Properties/AssemblyInfo.cs create mode 100644 TreeCompare/TreeCompare.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e82d27 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# Autosave files +*~ + +# build +[Oo]bj/ +[Bb]in/ +packages/ +TestResults/ + +# globs +Makefile.in +*.DS_Store +*.sln.cache +*.suo +*.cache +*.pidb +*.userprefs +*.usertasks +config.log +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.user +*.tar.gz +tarballs/ +test-results/ +Thumbs.db + +# Mac bundle stuff +*.dmg +*.app + +# resharper +*_Resharper.* +*.Resharper + +# dotCover +*.dotCover diff --git a/TreeCompare.sln b/TreeCompare.sln new file mode 100644 index 0000000..c557fdc --- /dev/null +++ b/TreeCompare.sln @@ -0,0 +1,17 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TreeCompare", "TreeCompare\TreeCompare.csproj", "{03ABFA64-A282-432E-A21A-A160A02611D1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03ABFA64-A282-432E-A21A-A160A02611D1}.Debug|x86.ActiveCfg = Debug|x86 + {03ABFA64-A282-432E-A21A-A160A02611D1}.Debug|x86.Build.0 = Debug|x86 + {03ABFA64-A282-432E-A21A-A160A02611D1}.Release|x86.ActiveCfg = Release|x86 + {03ABFA64-A282-432E-A21A-A160A02611D1}.Release|x86.Build.0 = Release|x86 + EndGlobalSection +EndGlobal diff --git a/TreeCompare/Program.cs b/TreeCompare/Program.cs new file mode 100644 index 0000000..aaf7fbf --- /dev/null +++ b/TreeCompare/Program.cs @@ -0,0 +1,254 @@ +using System; +using System.IO; +using System.Collections.Generic; + +namespace TreeCompare +{ + class MainClass + { + public static void Main(string[] args) + { + List leftFiles = new List(); + List rightFiles = new List(); + string progressBar = "-"; + long counter = 0; + List differences = new List(); + + Console.Error.WriteLine("TreeCompare v0.1 © 2017 Natalia Portillo"); + + if(args.Length != 2) + { + Console.Error.WriteLine("Usage: TreeCompare "); + return; + } + + if (args[0] == args[1]) + { + Console.Error.WriteLine("Directories must not be the same"); + return; + } + + if(!Directory.Exists(args[0])) + { + Console.Error.WriteLine("{0} does not exist or is not a directory", args[0]); + return; + } + + if (!Directory.Exists(args[1])) + { + Console.Error.WriteLine("{0} does not exist or is not a directory", args[1]); + return; + } + + DirectoryInfo dir0Info = new DirectoryInfo(args[0]); + DirectoryInfo dir1Info = new DirectoryInfo(args[1]); + string dir0 = Path.GetFullPath(args[0]); + string dir1 = Path.GetFullPath(args[1]); + + Console.Error.Write("Counting files in {0}", dir0); + foreach (string left in Directory.GetFiles(dir0, "*", SearchOption.AllDirectories)) + { + switch(counter % 20) + { + case 0: + case 1: + case 2: + case 3: + case 4: + progressBar = "-"; + break; + case 5: + case 6: + case 7: + case 8: + case 9: + progressBar = "\\"; + break; + case 10: + case 11: + case 12: + case 13: + case 14: + progressBar = "|"; + break; + case 15: + case 16: + case 17: + case 18: + case 19: + progressBar = "/"; + break; + } + Console.Error.Write("\rCounting files in {0}... {1}", dir0, progressBar); + string relative = Path.GetFullPath(left).Substring(dir0.Length); + if (relative[0] == '/') + relative = relative.Substring(1); + leftFiles.Add(relative); + counter++; + } + Console.Error.WriteLine(); + + counter = 0; + Console.Error.Write("Counting files in {0}", dir1); + foreach (string right in Directory.GetFiles(dir1, "*", SearchOption.AllDirectories)) + { + switch (counter % 20) + { + case 0: + case 1: + case 2: + case 3: + case 4: + progressBar = "-"; + break; + case 5: + case 6: + case 7: + case 8: + case 9: + progressBar = "\\"; + break; + case 10: + case 11: + case 12: + case 13: + case 14: + progressBar = "|"; + break; + case 15: + case 16: + case 17: + case 18: + case 19: + progressBar = "/"; + break; + } + Console.Error.Write("\rCounting files in {0}... {1}", dir1, progressBar); + string relative = Path.GetFullPath(right).Substring(dir1.Length); + if (relative[0] == '/') + relative = relative.Substring(1); + rightFiles.Add(relative); + counter++; + } + Console.Error.WriteLine(); + + counter = 0; + Console.Error.Write("Comparing files..."); + List leftNotInRight = new List(); + foreach (string left in leftFiles) + { + switch (counter % 20) + { + case 0: + case 1: + case 2: + case 3: + case 4: + progressBar = "-"; + break; + case 5: + case 6: + case 7: + case 8: + case 9: + progressBar = "\\"; + break; + case 10: + case 11: + case 12: + case 13: + case 14: + progressBar = "|"; + break; + case 15: + case 16: + case 17: + case 18: + case 19: + progressBar = "/"; + break; + } + Console.Error.Write("\rComparing files... {0}", progressBar); + + if (!rightFiles.Contains(left)) + { + leftNotInRight.Add(left); + continue; + } + + rightFiles.Remove(left); + FileInfo lfi = new FileInfo(Path.Combine(dir0, left)); + FileInfo rfi = new FileInfo(Path.Combine(dir1, left)); + + string ctimeDif; + string atimeDif; + string mtimeDif; + string attrDif; + string sizeDif; + bool sameContents = true; + + if (lfi.CreationTimeUtc == rfi.CreationTimeUtc) + ctimeDif = "Same"; + else + ctimeDif = string.Format("{0} - {1}", lfi.CreationTimeUtc, rfi.CreationTimeUtc); + + if (lfi.LastAccessTime == rfi.LastAccessTime) + atimeDif = "Same"; + else + atimeDif = string.Format("{0} - {1}", lfi.LastAccessTime, rfi.LastAccessTime); + + if (lfi.LastWriteTimeUtc == rfi.LastWriteTimeUtc) + mtimeDif = "Same"; + else + mtimeDif = string.Format("{0} - {1}", lfi.LastWriteTimeUtc, rfi.LastWriteTimeUtc); + + if (lfi.Attributes == rfi.Attributes) + attrDif = "Same"; + else + attrDif = string.Format("{0} - {1}", lfi.Attributes, rfi.Attributes); + + if (lfi.Length == rfi.Length) + { + sizeDif = "Same"; + byte[] lbuf = new byte[1048576]; + byte[] rbuf = new byte[1048576]; + FileStream lfs = lfi.Open(FileMode.Open, FileAccess.Read); + FileStream rfs = rfi.Open(FileMode.Open, FileAccess.Read); + + while (lfs.Read(lbuf, 0, lbuf.Length) > 0 && rfs.Read(rbuf, 0, rbuf.Length) > 0 && sameContents) + { + for (int i = 0; i < lbuf.Length; i++) + { + if(lbuf[i] != rbuf[i]) + { + sameContents = false; + break; + } + } + } + } + else + { + sizeDif = string.Format("{0} - {1}", lfi.Length, rfi.Length); + sameContents = false; + } + + differences.Add(string.Format("\"{0}\";{1};{2};{3};{4};{5};{6};{7};{8}", left, "Yes", "Yes", ctimeDif, atimeDif, mtimeDif, attrDif, sizeDif, sameContents ? "Same" : "Different")); + counter++; + } + Console.Error.WriteLine(); + + foreach (string left in leftNotInRight) + differences.Add(string.Format("\"{0}\";{1};{2};;;;;;", left, "Yes", "No")); + + foreach (string right in rightFiles) + differences.Add(string.Format("\"{0}\";{1};{2};;;;;;", right, "No", "Yes")); + + differences.Sort(); + + Console.WriteLine("Path;Exists in {0};Exists in {1};Creation time;Access time;Modification time;Attributes;Size;Contents", dir0, dir1); + foreach (string diff in differences) + Console.WriteLine("{0}", diff); + } + } +} diff --git a/TreeCompare/Properties/AssemblyInfo.cs b/TreeCompare/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9915b60 --- /dev/null +++ b/TreeCompare/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("TreeCompare")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Claunia.com")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Copyright © Claunia.com")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/TreeCompare/TreeCompare.csproj b/TreeCompare/TreeCompare.csproj new file mode 100644 index 0000000..c37a32b --- /dev/null +++ b/TreeCompare/TreeCompare.csproj @@ -0,0 +1,39 @@ + + + + Debug + x86 + {03ABFA64-A282-432E-A21A-A160A02611D1} + Exe + TreeCompare + TreeCompare + v4.6.1 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + \ No newline at end of file