locked
GetFiles - Exclude Certain File Types RRS feed

  • Question

  • The following code creates a list of all files of type .gif; how do I modify it to exclude certain .gif files? e.g. I don't want to include all .gif file that start with "testGif" (testGif_This is my file.gif")

    DirectoryInfo dirDirectory = new DirectoryInfo(MyFilepath);
    FileInfo[] currentFiles = Saved_Email_dirDirectory .GetFiles("*.gif");          
    List<FileInfo> lstFiles = new List<FileInfo>();

    Wednesday, March 23, 2016 6:27 PM

Answers

  • Hi Shayaan,

    >>how do I modify it to exclude certain .gif files? e.g. I don't want to include all .gif file that start with "testGif" (testGif_This is my file.gif")

    For your scenario, you could try the next code. I have tested and it works well.

    string MyFilepath1 = @"C:\Temp";
    
                DirectoryInfo dirDirectory1 = new DirectoryInfo(MyFilepath1);
    
             
    
                FileInfo[] currentFiles = dirDirectory.GetFiles("testGif*.gif");
    
    
                List<FileInfo> lstFiles = new List<FileInfo>();
    


    For more information about GetFiles method, please see the next link.

    https://msdn.microsoft.com/en-us/library/8he88b63(v=vs.110).aspx

    Note:

    “The search string to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.”

    Best regards,

    Alex

    • Proposed as answer by CoolDadTx Friday, March 25, 2016 5:46 PM
    • Marked as answer by Kristin Xie Tuesday, April 5, 2016 2:47 AM
    Thursday, March 24, 2016 7:45 AM

All replies

  • Looping through with something like this should work.

    foreach (var item in currentFiles)
    {
        if (!item.Name.StartsWith("testGif")) {
            lstFiles.Add(item);
        }
    }

    Wednesday, March 23, 2016 6:55 PM
  • Thanks, but this is not what I am looking to do and loop through thousands of files.

    Wednesday, March 23, 2016 7:27 PM
  • Hi Shayaan,

    >>how do I modify it to exclude certain .gif files? e.g. I don't want to include all .gif file that start with "testGif" (testGif_This is my file.gif")

    For your scenario, you could try the next code. I have tested and it works well.

    string MyFilepath1 = @"C:\Temp";
    
                DirectoryInfo dirDirectory1 = new DirectoryInfo(MyFilepath1);
    
             
    
                FileInfo[] currentFiles = dirDirectory.GetFiles("testGif*.gif");
    
    
                List<FileInfo> lstFiles = new List<FileInfo>();
    


    For more information about GetFiles method, please see the next link.

    https://msdn.microsoft.com/en-us/library/8he88b63(v=vs.110).aspx

    Note:

    “The search string to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.”

    Best regards,

    Alex

    • Proposed as answer by CoolDadTx Friday, March 25, 2016 5:46 PM
    • Marked as answer by Kristin Xie Tuesday, April 5, 2016 2:47 AM
    Thursday, March 24, 2016 7:45 AM
  • You should use the approach that Alex suggested, but I'd like to add an alternative just in case you need some additional filtering (for example based on when the file was created). With this you can filter out based on any FileInfo information:

    FileInfo[] currentFiles =
        Saved_Email_dirDirectory.EnumerateFiles("*.gif")
                                .Where(currentFile => currentFile.Name.StartsWith("testGif"))
                                .ToArray();
    Friday, March 25, 2016 8:08 AM