从分组文件路径集合中提取公共路径?

时间:2017-10-04 15:39:44

标签: c# string pattern-matching directory

我有一个与https://en.wikipedia.org/wiki/Longest_common_substring_problem相关的问题,我的源代码集包含一个文件路径列表,它们并不总是共享一条公共路径(有时在C:\驱动器之外)ex:

来源收集:

C:\Test\Root\Common\Data\a.txt
C:\Test\Root\Common\Data\Home\b.txt
C:\Test\Root\Common\Data\Home\Dev\c.txt
C:\Test2\Random\Data\a.txt
C:\Test2\Random\b.txt
C:\Test2\c.txt
D:\Data\a.txt

输出应该是一个集合:

C:\Test\Root\Common\Data\
C:\Test2\
D:\Data\

如何找到每个"组的共同路径"文件路径?我在这里找到了很多解决方案,但总是有一组文件路径共享至少一个公共目录,而不是这里的情况。

2 个答案:

答案 0 :(得分:1)

我仍然不确定我是否正确理解问题......

我希望这会奏效。

    public List<string> ExtractCommonPaths(List<string> paths)
    {
        var separatedImput = paths
            .Select(path => path.Split(new [] {":\\", "\\" }, StringSplitOptions.RemoveEmptyEntries))
            .Select(path => path.Take(path.Length - 1).ToList());
        return separatedImput.GroupBy(path => path[0] + ":\\" + path[1])
            .Select(g =>
            {
                var commonPath = g.Key;
                var commpoPathLength = 2;
                for (;;)
                {
                    var exit = false;
                    var pathItem = string.Empty;
                    foreach (var path in g)
                    {
                        if (path.Count <= commpoPathLength)
                        {
                            exit = true;
                            break;
                        }

                        if (pathItem == string.Empty)
                            pathItem = path[commpoPathLength];
                        else
                        {
                            if (pathItem != path[commpoPathLength])
                            {
                                exit = true;
                                break;
                            }
                        }
                    }

                    if (exit)
                        break;
                    commonPath += "\\" + pathItem;
                    commpoPathLength++;
                }

                return commonPath;
            })
            .ToList();
    }

答案 1 :(得分:0)

如果你想在某个位置查找目录,我有一些东西。 对于这种方法,我拥有来自某个 (C:\Files1) 示例位置的每个目录。

如果您只想从此列表中获取主目录:

public List<DirectoryInfo> ExtractDirectoriesCommonPaths(List<DirectoryInfo> directories, string location)
    {
        var result = new List<DirectoryInfo>() { };
        directories.ForEach(directory =>
        {
            var otherDirectories = directories.Where(d => d.FullName != directory.FullName);
            var anyDirectoryWithThisSamePath = otherDirectories.Where(x => directory.FullName.Contains(x.FullName) && x.FullName.Length < directory.FullName.Length);
            if (anyDirectoryWithThisSamePath.Any())
            {
                result.Add(anyDirectoryWithThisSamePath.FirstOrDefault());
            }
        });

        return result.Where(x => x.FullName != location && x.FullName.Length > location.Length).Distinct().ToList();
    }

输入:

C:\Files1\test_announcements
C:\Files1\Parent
C:\Files1\test_announcements\test_announcements_archive
C:\Files1\Parent\child
C:\Files1\Parent\child2
C:\Files1\Parent\child\subchild
C:\Files1\test_announcements
C:\Files1\Parent

输出:

C:\Files1\test_announcements
C:\Files1\Parent