Created
February 23, 2012 18:35
-
-
Save JeffJacobson/1894257 to your computer and use it in GitHub Desktop.
C# extension method for walking the file system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace FileSystemHelper | |
{ | |
public static class FileSystemExtensions | |
{ | |
/// <summary> | |
/// Walks the file system, similar to Python's os.walk function. | |
/// </summary> | |
/// <param name="root">The root directory where the walking starts.</param> | |
/// <param name="handler">A function that will be invoked for each directory.</param> | |
/// <param name="searchRegex">A regular expression used to filter the list of files. If a value is provided, <paramref name="searchPattern"/> will be ignored.</param> | |
/// <param name="searchPattern">The search pattern used to limit the files and directories.</param> | |
/// <param name="searchOption">Option to use when searching files and directories.</param> | |
/// <param name="searchPatternAppliesToDirectories">Determines if the <paramref name="searchPattern"/> and <paramref name="searchOption"/> are used on only the files or both the files and directories.</param> | |
/// <param name="ignoredFileRegex">Any file with a file name matching this <see cref="Regex"/> will be skipped. Only the file name (and not the full path) is checked against this <see cref="Regex"/>.</param> | |
/// <example><code>static void AddToFileList(DirectoryInfo root, DirectoryInfo[] directories, FileInfo[] files) | |
/// { | |
/// foreach (var file in files) | |
/// { | |
/// Console.WriteLine(file.FullName); | |
/// } | |
/// } | |
/// | |
/// static void Main(string[] args) | |
/// { | |
/// var root = new DirectoryInfo(@"C:\temp"); | |
/// root.Walk(AddToFileList); | |
/// }</code></example> | |
public static void Walk(this DirectoryInfo root, Action<DirectoryInfo, IEnumerable<DirectoryInfo>, IEnumerable<FileInfo>> handler, Regex searchRegex = null, | |
string searchPattern = null, SearchOption searchOption = SearchOption.AllDirectories, bool searchPatternAppliesToDirectories = false, Regex ignoredFileRegex = null) | |
{ | |
if (root == null) | |
{ | |
throw new ArgumentNullException("directory"); | |
} | |
IEnumerable<DirectoryInfo> directories = !searchPatternAppliesToDirectories || (searchRegex != null && searchPattern == null) ? root.GetDirectories() | |
: searchRegex != null ? root.GetDirectories().Where(d => searchRegex.IsMatch(d.Name)) | |
: root.GetDirectories(searchPattern, searchOption); | |
IEnumerable<FileInfo> files = | |
searchRegex == null && searchPattern == null | |
? root.GetFiles() : searchRegex != null ? root.GetFiles().Where(f => searchRegex.IsMatch(f.Name)) | |
: root.GetFiles(searchPattern, searchOption); | |
// Remove the ignored files from the list. | |
if (ignoredFileRegex != null) | |
{ | |
files = files.Where(f => !ignoredFileRegex.IsMatch(f.Name)); | |
} | |
handler.Invoke(root, directories, files); | |
foreach (var d in directories) | |
{ | |
d.Walk(handler, searchRegex, searchPattern, searchOption, searchPatternAppliesToDirectories, ignoredFileRegex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment