Update Program.cs
Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
37
Program.cs
37
Program.cs
@@ -20,17 +20,28 @@ namespace DuplicateFinder
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
static string GetValidDirectoryPath()
|
||||
{
|
||||
Console.Write("Enter directory path: ");
|
||||
string directoryPath = Console.ReadLine();
|
||||
string path = Console.ReadLine().Trim();
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Console.WriteLine("The directory does not exist. Please try again.");
|
||||
return GetValidDirectoryPath();
|
||||
}
|
||||
Console.WriteLine();
|
||||
return path;
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
string directoryPath = GetValidDirectoryPath();
|
||||
|
||||
Dictionary<string, List<string>> fileHashes = new Dictionary<string, List<string>>();
|
||||
|
||||
foreach (string filePath in Directory.EnumerateFiles(directoryPath, "*.*", SearchOption.AllDirectories))
|
||||
{
|
||||
Console.WriteLine("Calculating hash: " + filePath);
|
||||
string hash = CalculateSha1(filePath);
|
||||
|
||||
if (fileHashes.ContainsKey(hash))
|
||||
{
|
||||
fileHashes[hash].Add(filePath);
|
||||
@@ -41,16 +52,24 @@ namespace DuplicateFinder
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("\nDuplicate files:");
|
||||
foreach (var group in fileHashes.Values.Where(v => v.Count > 1))
|
||||
if (fileHashes.Values.Count(v => v.Count > 1) > 1)
|
||||
{
|
||||
foreach (var filePath in group)
|
||||
Console.WriteLine("\nDuplicate files:");
|
||||
foreach (List<string> group in fileHashes.Values.Where(v => v.Count > 1))
|
||||
{
|
||||
Console.WriteLine(filePath);
|
||||
foreach (string filePath in group)
|
||||
{
|
||||
Console.WriteLine(filePath);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("\nDuplicates not find.");
|
||||
}
|
||||
|
||||
Console.WriteLine("Press any key to exit...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user