-
June 9th, 2005, 09:22 AM
#1
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
-
June 9th, 2005, 01:42 PM
#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.
-
June 9th, 2005, 02:26 PM
#3
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.
-
June 9th, 2005, 03:04 PM
#4
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
-
June 10th, 2005, 05:41 AM
#5
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
-
February 8th, 2008, 02:21 PM
#6
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.
-
June 10th, 2011, 09:38 AM
#7
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
Originally Posted by MadHatter
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:\\");
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|