Created
May 3, 2013 18:09
-
-
Save kbrammer/5512188 to your computer and use it in GitHub Desktop.
Extension methods to enumerate files and directories, include or exclude by name or extension, and search file contents. Example is in LINQPad format.
This file contains 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
void Main() | |
{ | |
string path = Path.GetFullPath(@"C:\Users\Username"); | |
System.IO.DirectoryInfo di = new DirectoryInfo(path); | |
var query = di.GetFiles("*.*",SearchOption.TopDirectoryOnly).IncludeFileExtensions("*.asp").SearchFileContents("searchterm"); | |
query.Dump(); | |
} | |
public static class FileIOExtensions | |
{ | |
//Search File Contents | |
public static IEnumerable<KeyValuePair<string,string>> SearchFileContents(this FileInfo[] files, string text) | |
{ | |
return files.SelectMany(f => System.IO.File.ReadLines(f.FullName).Where (l => l.ToLower().Contains(text)).Select (l => new KeyValuePair<string, string>(f.FullName,l))); | |
} | |
#region File Extensions | |
//Example: FileInfo[] pages = folderInfo.GetFiles("*.*").WithExtension(".aspx", ".aspx", ".htm", ".html"); | |
public static FileInfo[] ExcludeFileExtensions(this FileInfo[] files, params string[] args) | |
{ | |
if (args == null) | |
throw new ArgumentNullException("args"); | |
return args.SelectMany (a => files.Where(f => !a.ToLowerInvariant().Contains(f.Extension.ToLowerInvariant()))).ToArray(); | |
} | |
public static FileInfo[] IncludeFileExtensions(this FileInfo[] files, params string[] args) | |
{ | |
if (args == null) | |
throw new ArgumentNullException("args"); | |
return args.SelectMany (a => files.Where(f => a.ToLowerInvariant().Contains(f.Extension.ToLowerInvariant()))).ToArray(); | |
} | |
#endregion | |
#region File Names | |
//Example: FileInfo[] pages = folderInfo.GetFiles("*.*").IncludeFileNames("index.aspx", "default.aspx", "index.htm", "index.html"); | |
public static FileInfo[] ExcludeFileNames(this FileInfo[] files, params string[] args) | |
{ | |
if (args == null) | |
throw new ArgumentNullException("args"); | |
return args.SelectMany (a => files.Where(f => !a.ToLowerInvariant().Contains(f.Name.ToLowerInvariant()))).ToArray(); | |
} | |
public static FileInfo[] IncludeFileNames(this FileInfo[] files, params string[] args) | |
{ | |
if (args == null) | |
throw new ArgumentNullException("args"); | |
return args.SelectMany (a => files.Where(f => a.ToLowerInvariant().Contains(f.Name.ToLowerInvariant()))).ToArray(); | |
} | |
#endregion | |
#region Directory Names | |
//Example: foreach (DirectoryInfo folder in folders.ExcludeDirectoryNames("app_code", "app_data", "bin", "assets", "images")) | |
public static DirectoryInfo[] ExcludeDirectoryNames(this DirectoryInfo[] dirs, params string[] args) | |
{ | |
if (args == null) | |
throw new ArgumentNullException("args"); | |
return args.SelectMany (a => dirs.Where(d => !a.ToLowerInvariant().Contains(d.Name.ToLowerInvariant()))).ToArray(); | |
} | |
public static DirectoryInfo[] IncludeDirectoryNames(this DirectoryInfo[] dirs, params string[] args) | |
{ | |
if (args == null) | |
throw new ArgumentNullException("args"); | |
return args.SelectMany (a => dirs.Where(d => a.ToLowerInvariant().Contains(d.Name.ToLowerInvariant()))).ToArray(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dmitriymarushko Thanks!