Exemplo n.º 1
0
        public static List <FileInfo> Find(string rootDirectory, FileFinderOptions options)
        {
            var result    = new List <FileInfo>();
            var directory = new DirectoryInfo(rootDirectory);

            Scan(directory, options, result);
            return(result);
        }
Exemplo n.º 2
0
        private static void Scan(DirectoryInfo sourceDirectory, FileFinderOptions options, ICollection <FileInfo> result)
        {
            if (!sourceDirectory.Exists)
            {
                throw new Exception($"Source directory does not exist or could not be found: {sourceDirectory}");
            }

            var fileInfos = new List <FileInfo>();

            foreach (var fileInfo in sourceDirectory.GetFiles())
            {
                var file = fileInfo;
                if (options.Excludes.Any(pattern => Matches(pattern, file.FullName)))
                {
                    continue;
                }

                fileInfos.Add(fileInfo);
            }

            var directoryInfos = sourceDirectory.GetDirectories();

            if ((!fileInfos.Any() && !directoryInfos.Any()) || options.Excludes.Any(pattern => Matches(pattern, sourceDirectory.FullName)))
            {
                return;
            }

            foreach (var subDirectory in directoryInfos)
            {
                Scan(subDirectory, options, result);
            }

            foreach (var file in fileInfos)
            {
                try
                {
                    var fileName = file.FullName;

                    if (options.Excludes.Any(pattern => Matches(pattern, fileName)))
                    {
                        continue;
                    }

                    if (options.Includes.Any(pattern => Matches(pattern, fileName)))
                    {
                        result.Add(file);
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception($"{file.FullName} failed to scan: {exception.Message}");
                }
            }
        }