Marcus Tan
Marcus Tan

Reputation: 417

c# Linq compare file with same file name without extension

Hi i had trying to compare 2 folder to check whether there are same file name exist in both folder or not. Is there any simple way to direct search it without comparing the file name only without the extension?

As Folder A's files are .MAP and Folder B's files are .IJM (I just wanted to compare the file name only) Because it may exist ABC.MAP and ABC.IJM in both the folder.

So far the code I tried was as below:

// Create two identical or different temporary folders   
// on a local drive and change these file paths.  
string pathA = @"C:\Users\ADMIN\Desktop\POOLING PATH";
string pathB = @"C:\Users\ADMIN\Desktop\PROCESS PATH";

System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);

// Take a snapshot of the file system.  
IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

foreach (var test in list1)
{
    richTextBox1.AppendText(Path.GetFileNameWithoutExtension(test.FullName) + "\n");
}
foreach (var test in list2)
{
    richTextBox1.AppendText(Path.GetFileNameWithoutExtension(test.FullName) + "\n");
}

Upvotes: 1

Views: 896

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

Well, you can try a Linq query, e.g.

 // *.map, *.ijm: 
 // ...As Folder A's files are .MAP and Folder B's files are .IJM
 var result = Directory
   .EnumerateFiles(pathA, "*.map", SearchOption.AllDirectories) 
   .Select(A => Path.GetFileNameWithoutExtension(A)) 
   .Intersect(Directory 
      .EnumerateFiles(pathB, "*.ijm", SearchOption.AllDirectories) 
      .Select(B => Path.GetFileNameWithoutExtension(B)),
       StringComparer.OrdinalIgnoreCase) 
   .OrderBy(name => name); 

followed by

  richTextBox1.AppendText(string.Join(Environment.NewLine, result));

Edit: if you want to play with sets (see comments below), let's collect files into HashSet<string>s:

  HashSet<string> filesA = new HashSet<string>(Directory
      .EnumerateFiles(pathA, "*.map", SearchOption.AllDirectories) 
      .Select(A => Path.GetFileNameWithoutExtension(A)),
    StringComparer.OrdinalIgnoreCase);

  HashSet<string> filesB = new HashSet<string>(Directory
      .EnumerateFiles(pathB, "*.ijm", SearchOption.AllDirectories) 
      .Select(B => Path.GetFileNameWithoutExtension(B)),
    StringComparer.OrdinalIgnoreCase);

Now, you can put:

  // filesA has only files that are in filesB as well
  // filesA.IntersectWith(filesB);

  // filesA has only files that are NOT in filesB
  // filesA.ExceptWith(filesB);

  // filesB has only files that are NOT in filesA
  // filesB.ExceptWith(filesA);

  // filesA has only files that are either in filesA or in filesB (not in both)
  //filesA.SymmetricExceptWith(filesB)

Upvotes: 3

ArcX
ArcX

Reputation: 817

Note this code is untested, but sounds like you're looking for an intersect?

So maybe something like this...

var pathA = @"C:\Users\ADMIN\Desktop\POOLING PATH";
var pathB = @"C:\Users\ADMIN\Desktop\PROCESS PATH";

List<string> GetFiles(string path) => Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly).Select(x => Path.GetFileNameWithoutExtension(x.Name)).ToList();

var filesA = GetFiles(pathA);
var filesB = GetFiles(pathB);

// should be a list of file names found in both lists
var common = filesA.Intersect(filesB).ToList();

Upvotes: 1

Related Questions