CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Posts
    4

    Question DirectoryInfo.GetFiles() multiple file extensions

    Hello,

    is there a possibility to get files with multiple file extensions?
    I need something like

    DirectoryInfo myDirectory = new DirectoryInfo("c:\MyDir")
    FileSystemInfo[] myReceivedFiles = myDirectory.GetFiles("*.aaa;*.bbb")

    but unfortunately that doesn't work.


    Thank you

    sunax

  2. #2

    Re: DirectoryInfo.GetFiles() multiple file extensions

    There's not a way to do this with the default object.

    The easiest way to do it may be something like:

    Code:
    string lookfor = "*.txt;*.zip;*.exe";
    string[] extensions = lookfor.Split(new char[] {';'});
    string mydirectory = @"c:\tmp";
    
    ArrayList myfileinfos = new ArrayList();
    DirectoryInfo di = new DirectoryInfo(mydirectory);
    
    foreach (string ext in extensions)
    {
       myfileinfos.AddRange(di.GetFiles(ext));
    }
    
    FileInfo[] allfileinfo = myfileinfos.ToArray(typeof(FileInfo));
    You can obviously change that around a little bit to get the desired result. Ideally you could just inherit from the class and create your own, but the DirectoryInfo class is sealed.

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: DirectoryInfo.GetFiles() multiple file extensions

    Code:
    namespace PathSearch {
        public class FileSearch {
            ArrayList _extensions;
            bool _recursive;
            public ArrayList SearchExtensions {
                get { return _extensions; }
            }
            public bool Recursive {
                get{return _recursive;}
                set{_recursive = value;}
            }
            public FileSearch(){
                _extensions = ArrayList.Synchronized(new ArrayList());
                _recursive = true;
            }
            public FileInfo[] Search(string path) {   
                DirectoryInfo root = new DirectoryInfo(path);
                ArrayList subFiles = new ArrayList();
                foreach(FileInfo file in root.GetFiles()) {
                    if(_extensions.Contains( file.Extension )) {
                        subFiles.Add(file);
                    }
                }
                if(_recursive) {
                    foreach(DirectoryInfo directory in root.GetDirectories()) {
                        subFiles.AddRange( Search(directory.FullName) );
                    }
                }
                return (FileInfo[])subFiles.ToArray(typeof(FileInfo));
            }
        }
    }
    usage:

    Code:
    FileSearch searcher = new FileSearch();
    searcher.SearchExtensions.Add(".dll");
    searcher.SearchExtensions.AddRange( new string[] {".txt", ".asp", ".exe"} );
    
    FileInfo[] files = searcher.Search("C:\\");
    Last edited by MadHatter; June 9th, 2005 at 02:28 PM.

  4. #4
    Join Date
    Jun 2005
    Posts
    4

    Smile Re: DirectoryInfo.GetFiles() multiple file extensions

    Thank you mmetzger, thank you MadHatter,

    that is exactly what I was looking for and much better than the function I wrote :-)

    Thank you for your help!!!

    sunax

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: DirectoryInfo.GetFiles() multiple file extensions

    by the way, in java, we can make a file filter object that has a method public boolean accept(String filename)

    it is soemthing you customise, and is used something like this:

    Code:
    File[] files = myDir.listFiles(
      new FileFilter(){
        public boolean accept(String filename){
          return filename.matches(".*[.]mp3|.*[.]wav|.*[.]cue");
        }
      }
    );
    now, i know you cant decalre classes inline in c#, but seeing this might jog someone's memory if an equivalent is available. the dir listing visits accept() for every file in the dir and its only returned in the array if it passes (true)

    essentially what these guys have here, but shorthand
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Jun 2011
    Posts
    1

    Re: DirectoryInfo.GetFiles() multiple file extensions

    I know this has been years but this bit of code has sped up my code tremendously. The only problem I ran into is if the filename is longer that 255 it throws an error. I tried a catch but I think the getfiles is where the problem is and I don't know how to handle that. Any ideas on how to handle filenames/paths that are longer than 255, I don't care if it skips them I just need to figure out how to do it. Thanks

    Quote Originally Posted by MadHatter View Post
    Code:
    namespace PathSearch {
        public class FileSearch {
            ArrayList _extensions;
            bool _recursive;
            public ArrayList SearchExtensions {
                get { return _extensions; }
            }
            public bool Recursive {
                get{return _recursive;}
                set{_recursive = value;}
            }
            public FileSearch(){
                _extensions = ArrayList.Synchronized(new ArrayList());
                _recursive = true;
            }
            public FileInfo[] Search(string path) {   
                DirectoryInfo root = new DirectoryInfo(path);
                ArrayList subFiles = new ArrayList();
                foreach(FileInfo file in root.GetFiles()) {
                    if(_extensions.Contains( file.Extension )) {
                        subFiles.Add(file);
                    }
                }
                if(_recursive) {
                    foreach(DirectoryInfo directory in root.GetDirectories()) {
                        subFiles.AddRange( Search(directory.FullName) );
                    }
                }
                return (FileInfo[])subFiles.ToArray(typeof(FileInfo));
            }
        }
    }
    usage:

    Code:
    FileSearch searcher = new FileSearch();
    searcher.SearchExtensions.Add(".dll");
    searcher.SearchExtensions.AddRange( new string[] {".txt", ".asp", ".exe"} );
    
    FileInfo[] files = searcher.Search("C:\\");

  7. #7
    Join Date
    Feb 2008
    Posts
    1

    Resolved Re: DirectoryInfo.GetFiles() multiple file extensions

    since the last answer to this post have passed long time (3 years);

    I needed to do the same as you want and to accomplish it i used a Generic list of FileInfo objects, then applied a filter using a delegate to the FindAll method, here is the code:
    Code:
    using System;
    using System.IO ;
    using System.Collections.Generic;
    using System.Text;
    
    namespace FileFilterTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                DirectoryInfo di = new DirectoryInfo("c:\\");
                // An empty list of FileInfo objects
                List<FileInfo> lFi = new List<FileInfo>();
    
                //Add the files of the directory to the list
                lFi.AddRange(di.GetFiles());
    
                //Find the files on the list using a delegate
                lFi = lFi.FindAll(delegate(FileInfo f) { return f.Extension.ToLower() == ".aaa" || f.Extension.ToLower() == ".bbb"; }); 
    
                //Iterate over each filtered file 
                foreach (FileInfo fi in lFi)
                    Console.WriteLine(fi.FullName);
    
                Console.ReadLine();
            }
        }
    }
    I hope this helps you, to compile this you need at least the .NET Framework 2.0 (VS2005) /3.0 (VS2005/2008)/3.5(VS2008) because the generics are not available on 1.1

    regards,

    coloboxp
    Last edited by coloboxp; February 8th, 2008 at 02:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured