The “GetFiles” function, returns all files in the given folder, including subfolders, that match the “includeMask”. The result is sorted by the file CreationTime. The function excludes all files that match the “excludeMask”

 

To call the function:

List<FileInfo> files = FileSystemHelper.GetFiles(@"C:\Windows","*.*", "*.exe", SortOrder.Ascending, SearchOption.AllDirectories);

 

The function:
/// <summary>
///
Get files in a folder sorted by creation datetime and filtered by include mask and exclude mask
/// </summary>
/// <param name=”folder”>
The folder containing the files</param>
/// <param name=”includeMask”>
Include files that meet the given mask</param>
/// <param name=”excludeMask”>
Exclude files that meet the given mask</param>
/// <param name=”sortOrder”>
Sort order, ascending or descending</param>
/// <param name=”searchOption”>
AllDirectories or TopDirectoryOnly</param>
/// <returns></returns>
public staticList<FileInfo> GetFiles(string folder, string includeMask, string excludeMask, SortOrder sortOrder, SearchOption searchOption)
{
    DirectoryInfo folderDirectoryInfo = newDirectoryInfo(folder);
    List<FileInfo> files = newList<FileInfo>(folderDirectoryInfo.GetFiles(includeMask, searchOption));
   
    stringexcludeMaskRegEx = FileSystemHelper.WildcardToRegex(excludeMask);

    // exclude files
  
files = (fromfile infiles
             where!Regex.IsMatch(file.Name, excludeMaskRegEx)
             selectfile).ToList<FileInfo>();

    if(files.Count > 0)
    {
        switch(sortOrder)
        {
            caseSortOrder.Ascending:
                // Sort files oldest first
              
files.Sort(
                    delegate(FileInfo firstFile, FileInfo secondFile)
                    {
                        returnfirstFile.CreationTime.CompareTo(secondFile.CreationTime);
                    }
                );
                break;
            caseSortOrder.Descending:
                // Sort files newest first
              
files.Sort(
                    delegate(FileInfo firstFile, FileInfo secondFile)
                    {
                        returnsecondFile.CreationTime.CompareTo(firstFile.CreationTime);
                    }
                );
                break;
        }

    }

    returnfiles;
}

 

/// <summary>
/// Convert a wildcard mask to regular expression
/// </summary>
/// <param name="pattern">wildcard string</param>
/// <returns>Regular expression string</returns>
public static string WildcardToRegex(string pattern)
{
    return string.Format("^{0}$", Regex.Escape(pattern).Replace("\*", ".*").Replace("\?", "."));
}

public enum SortOrder
{
        Ascending,
        Descending
}

One Comment

  1. Pingback: Interesting GetFiles to get a list of files with both an includeMask and excludeMask by Roel van Lisdonk « The Wiert Corner – irregular stream of stuff

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.