Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# - Finding the common files between two directories

Options
  • 18-02-2008 9:41pm
    #1
    Registered Users Posts: 2,835 ✭✭✭


    Lads,

    I'm have two directory paths, and i'm looking for a way to find the FileInfo objects that are common and the fileInfo objects that arn't between the two directories.

    Does anyone know how to go about this? I'm looking for way way of finding the intersection of the two directories if you get me...

    Thanks in advance!


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    What sort of container do you keep the FileInfo objects in? Are you using .Net 3.5? You might be able to use LINQ to do this very quickly.


  • Registered Users Posts: 2,835 ✭✭✭StickyMcGinty


    yup, using .NET 3.5

    I'm keeping them in a FileInfo array so i can iterate through them quickly.

    Then I want to be able to pass the common FileInfo objects accross methods.

    I've googled LINQ and it looks like it might help me out, but I cant seem to design my own file comparer? I want to compare files by name, md5 hash and date modified.

    Thankjs for the help so far fasty.


  • Registered Users Posts: 981 ✭✭✭fasty


    If they're in arrays, then you're sorted.

    Given two arrays of FileInfo objects fileInfo1 and fileInfo2...
    var folderDiff = (from f1 in fileInfo1 select f1.FullName).Except(from f2 in fileInfo2 select f2.FullName);
    

    ... you will be returned a container with all the differences based on filename.

    I don't have time to test, but I'm I don't think you'll be able to compare FileInfo objects directly, since they'd have different paths, but you could use anonymous types to pull the filename, modified date etc in the select part of the LINQ expression.


  • Registered Users Posts: 2,835 ✭✭✭StickyMcGinty


    fasty, thank you so much man.

    I'll give it a go anyway, the way i'm implementing it now is ridiculously complicated, thanks again!


  • Registered Users Posts: 981 ✭✭✭fasty


    No worries! BTW, I just had a mess about and it's Intersect to see what's common between two LINQ expressions!

    Excuse the quick and dirty sample full of static methods.
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace FileCompareWithLinq
    {
        class Program
        {
            static FileInfo[] ListDirectory(string path)
            {
                return (new DirectoryInfo(path)).GetFiles();
            }
    
            static void Main(string[] args)
            {
                FileInfo[] fileList1 = ListDirectory("C:\\Path\\One\\Here");
                FileInfo[] fileList2 = ListDirectory("C:\\Path\\Two\\Here");
    
                var filediff = (from f1 in fileList1 select f1.Name).Intersect(from f2 in fileList2 select f2.Name);
    
                foreach (String s in filediff)
                    Console.WriteLine(s);
    
                Console.ReadLine();
            }
        }
    }
    


  • Advertisement
  • Registered Users Posts: 2,835 ✭✭✭StickyMcGinty


    cheers my man, just threw together the same myself and i re-searched the array to get the FileInfo object so I can perform any comparision I want.

    This problem was kickin my ass all day too, so thanks again!


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    For more LINQ stuff, download LINQPad (its free)

    Allows to write quick and custom LINQ code very easily


Advertisement