Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract common file path from list of file paths in c#

Tags:

string

c#

What is the best way extract the common file path from the list of file path strings in c#?

Eg: I have a list 5 file paths in List variable, like below

c:\abc\pqr\tmp\sample\b.txt
c:\abc\pqr\tmp\new2\c1.txt
c:\abc\pqr\tmp\b2.txt
c:\abc\pqr\tmp\b3.txt
c:\abc\pqr\tmp\tmp2\b2.txt

output should be c:\abc\pqr\tmp

like image 921
Mahender Avatar asked Dec 20 '11 15:12

Mahender


2 Answers

Because everything is best solved with LINQ*:
*not everything is best solved with LINQ.

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

class Program
{
    static void Main(string[] args)
    {
        List<string> Files = new List<string>()
        {
            @"c:\abc\pqr\tmp\sample\b.txt",
            @"c:\abc\pqr\tmp\new2\c1.txt",
            @"c:\abc\pqr\tmp\b2.txt",
            @"c:\abc\pqr\tmp\b3.txt",
            @"c:\a.txt"
        };

        var MatchingChars =
            from len in Enumerable.Range(0, Files.Min(s => s.Length)).Reverse()
            let possibleMatch = Files.First().Substring(0, len)
            where Files.All(f => f.StartsWith(possibleMatch))
            select possibleMatch;

        var LongestDir = Path.GetDirectoryName(MatchingChars.First());
    }
}

Explanation:

The first line gets a list of lengths of possible matches to evaluate. We want the longest possibility first (so i reverse the enumeration which would be 0, 1, 2, 3; turning it into 3, 2, 1, 0).

I then get the string to match, which is simply a substring of the first entry of the given length.

I then filter the results, to ensure we only include possible matches that all files start with.

Finally, i return the first result, which will be the longest substring and call path.getdirectoryname to ensure if something has a few identical letters in the filenames it isn't included.

like image 176
George Duckett Avatar answered Oct 11 '22 11:10

George Duckett


I don't think there's a "trick"to get past using the brute force method to do this comparison, but you can optimize your solution somewhat:

Prepare:
Find the shortest string

Repeat:
See if all of the other strings contain it
If so, you're done
If not, remove one or more characters
like image 31
Matt Mills Avatar answered Oct 11 '22 13:10

Matt Mills