Extending the Directory.GetFiles() Filter Pattern

I recently wrote a little application which uploads specific files on a specific FTP. I came across the System.IO.Directory.GetFiles() Method and noticed that it’s natively only possible to add one kind of pattern. Which means:

string[] files = Directory.GetFiles(path, "*");

Works without problems. But what if you like to insert multiply extensions like:

string[] files = Directory.GetFiles(path, "*.jpg *.png *.gif");

This won’t work that way! The function only accepts one filter. And that’s how to add more than one:

string strFilter = "*.jpg;*.png;*.gif";
string[] m_arExt = strFilter.Split(';');
foreach(string filter in m_arExt)
{
    string[] strFiles = Directory.GetFiles(folder, filter);
}

ak

10 comments ↓

#1 Rufio on 08.09.06 at 8:12 pm

Hey; great help.

I assume we figure out for our own that we should add up all the strFiles to a seperate array, otherwise in the end strFiles will only have the files belonging to the final filter (assuming strFiles be declared before the for loop)? correct? or am I foolishly missing something?

#2 Sean Vikoren on 05.25.07 at 11:04 am

Thank you for the tip!

This is what I ended up using:

public static string [] GetVideoList (string path)
{
string filter = "*.mpg;*.gif" ;
string [] filterSplit = filter.Split (';') ;
int filterCount = filterSplit.Length ;
string [] fileList = null ;
string [][] jaggedFileList = new string[filterCount][] ;
int fileCount = 0 ;
int jaggedCount ;

if (Directory.Exists (path))
{
for (int i = 0; i

#3 Tim Cartwright on 01.31.08 at 10:04 pm

I actually streamlined your code a bit. Here take a look at this:


string folder = Path.GetDirectoryName(Server.MapPath(rotatorImage.ImageUrl));

string[] filters = {"*.jpg", "*.png" , "*.gif"};
ArrayList images = new ArrayList();


foreach(string filter in filters){
	images.AddRange(Directory.GetFiles(folder, filter));
}
#4 andreas.kraus on 02.01.08 at 10:19 am

Thanks Tim!

#5 ac on 04.05.08 at 1:51 am

Good HowTo!
Does anyone know of an example of binding it to a datagrid?

#6 WeldJla on 05.16.08 at 9:30 am

this works is good but there something missed.

When you apply two filters like (*.jpg; *.*), you can get the same files many times in the arrayList (in this case all jpg files will appears 2 times: one for *.jpg and one for *.*).

Then we need to add a function like DISTINCT() to make the list just needed.

thx.

#7 Chris on 05.16.08 at 10:41 am

This example is exactly what i’m looking for however very new to .NET, would anyone have the full working code – with it doing the databind bit

basically the complete start to finish code – i can compare and see where i’ve gone wrong

Thanks

#8 VS on 12.11.08 at 7:29 pm

This is not updating the array list !! Any ideas??

#9 fizzbin on 03.30.09 at 1:03 am

[code] public string ExtensionFilter {
get { return string.Join(“;”, _extentionFilters); }
set { _extentionFilters = value.Split(‘;’); }
}

private string[] _extentionFilters;[/code]

#10 Upender on 02.22.10 at 9:21 am

string[] files = Directory.GetFiles(path, “*”+urSearchstring+”*”);

Leave a Comment