Find the same files on disk

  • Thread starter Alexander Vasilevsky
  • Start date
A

Alexander Vasilevsky

Here, there is a challenge: to find the files must be on a local computer,
have the same names.

Get drives and folders to go through without problems. But what and how to
store files while a search?

http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
gift
 
C

Christof Nordiek

Alexander Vasilevsky said:
Here, there is a challenge: to find the files must be on a local computer,
have the same names.

Get drives and folders to go through without problems. But what and how to
store files while a search?
either List<string> or List<FileInfo>

Christof
 
M

Marc Gravell

It depends how robust it needs to be... if this is a one-off task,
then a simple dictionary over the name (key) and list of matching
paths (value) would probably suffice, but it wouldn't be very elegant
(or efficient) and would have a lot of false-positives without more
restriction than just the name. Very crude, but it works.

Marc

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
// lazy hacky static lookups
static readonly Dictionary<string, List<string>> allFiles = new
Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
static int counter = 0;

static void Main()
{
// find available drives
List<string> fixedDisks = new List<string>();
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.DriveType == DriveType.Fixed)
{
fixedDisks.Add(drive.Name);
}
}
Walk(fixedDisks.ToArray());

// output duplicates
foreach (KeyValuePair<string, List<string>> pairs in allFiles)
{
if (pairs.Value.Count > 1)
{
Console.WriteLine("***" + pairs.Key);
foreach (string path in pairs.Value)
{
Console.WriteLine(path);
}
}
}
}

static void Walk(string[] paths)
{
// expolde the gives paths
for(int i = 0; i < paths.Length; i++) {
string path = paths;
if ((counter++ % 100) == 0)
{ // cheap progress indicator
Console.WriteLine(path);
}

try
{// look at files
string[] files = Directory.GetFiles(path);
for (int j = 0; j < files.Length; j++)
{
string file = files[j], key =
Path.GetFileName(file);
List<string> lookups;
if (!allFiles.TryGetValue(key, out lookups))
{
lookups = new List<string>();
allFiles.Add(key, lookups);
}
lookups.Add(file);
}
}
catch(Exception ex)
{
Console.Error.WriteLine(path + ":" + ex.Message);
}

try
{ // look at sub-dirs
Walk(Directory.GetDirectories(path));
}
catch (Exception ex)
{
Console.Error.WriteLine(path + ":" + ex.Message);
}
}
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

A dictionary, the key is the name of the file the value the number of times
that you found that file.

I hope you know how to traverse a folder struct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top