Skip to content

Instantly share code, notes, and snippets.

@kbrammer
Created May 3, 2013 18:09
Show Gist options
  • Save kbrammer/5512188 to your computer and use it in GitHub Desktop.
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.
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
}
@dmitriymarushko
Copy link

dmitriymarushko commented Jan 5, 2023

ExcludeDirectoryNames doesn't work for me.
Correct version would be the following:

public static IEnumerable<string> ExcludeDirectoryNames(this IEnumerable<string> dirs, params string[] args) {

        if (args == null)
            throw new ArgumentNullException("args");
        return dirs.Where(x => args.All(q => !x.Contains(q, StringComparison.InvariantCultureIgnoreCase)));
    }

@kbrammer
Copy link
Author

kbrammer commented Jan 6, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment