public void Search(GlobPattern pattern, GlobPath curPath, List<GlobPath> result, ResultType resultType) { /* if (resultType == ResultType.Directory && pattern.IsOnLastPart) { //If the current directory matches the pattern, add this path to the result list if (pattern.MatchesCompletely(curPath)) result.Add(curPath.ToString()); }*/ if (resultType == ResultType.File && pattern.IsOnLastPart) { //Add all matching files to the result list foreach (var file in GetFiles(curPath)) { if (pattern.MatchesCurrentPart(file)) result.Add(curPath.AppendPart(file)); } } if (pattern.Parts[pattern.CurrentIndex] == ".." || pattern.Parts[pattern.CurrentIndex] == ".") { Search(pattern.SwitchToNextPart(), curPath.AppendPart(pattern.Parts[pattern.CurrentIndex]), result, resultType); return; } //If the next pattern part is recursive wildcard //we get list of all sub folders and continue the normal search there. if (pattern.Parts[pattern.CurrentIndex] == "**") { foreach (var subDir in GetRecursiveSubDirectories(curPath)) Search(pattern.SwitchToNextPart(), subDir, result, resultType); return; } //Traverse all sub directories which should be traversed foreach (var directory in GetDirectories(curPath)) { if (pattern.MatchesCurrentPart(directory)) if (pattern.IsOnLastPart && resultType == ResultType.Directory) { var newPath = curPath.AppendPart(directory); //Only add this path to result list if there is no longer version of this path result.RemoveAll(x => { if (x.Parts.Length <= newPath.Parts.Length) if (x.Parts.SequenceEqual(newPath.Parts.Take(x.Parts.Length))) return true; return false; }); result.Add(newPath); } else if (!pattern.IsOnLastPart) Search(pattern.SwitchToNextPart(), curPath.AppendPart(directory), result, resultType); } }
private void GetRecursiveSubDirectoriesInternal(GlobPath curPath, List<GlobPath> result) { foreach (var subDir in GetDirectories(curPath)) { var newPath = curPath.AppendPart(subDir); result.Add(newPath); GetRecursiveSubDirectoriesInternal(newPath, result); } }
private string[] GetDirectories(GlobPath path) { return Directory .GetDirectories(path.ToString()) .Select(ExtractLastPart) .ToArray(); }
private GlobPath[] GetRecursiveSubDirectories(GlobPath curPath) { List<GlobPath> result = new List<GlobPath> {curPath}; GetRecursiveSubDirectoriesInternal(curPath, result); return result.ToArray(); }