Directory.GetFiles Filter

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
129
Location
VA
Programming Experience
10+
I'm looping through directories to get a list of all the files in a web folder. I'd like to return only files with certain extensions such as .htm, .html, .asp, .aspx, etc.

From what I can tell, when doing a GetFiles(directory.FullName, Filter) the Filter can only contain something like "*.aspx". How do I filter, efficiently, for certain extensions?

I am currently or'ing together a bunch of EndsWith on the file names but there has to be a better way! :)
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
If ValidExtensions.IndexOf(fil.Extension) > -1 Then

changes to

If ValidExtensions.IndexOf(fil.Extension & "|") > -1 Then

Notice that in the ValidExtensions string each file type was delimited by a "|" character. Adding this character to the file extension will allow absolute matches. For example, instead of matching .js the match will be for .js| which will not provide a positive match for .jsp.

Let me know how this works out, I may need it in the future :)
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
129
Location
VA
Programming Experience
10+
Works like a champ! I'm wrapping up this app I'm working on, you'll see it in action soon as I'll be announcing it here and on the BV forums. I think you'll see the value of what's coming! :)
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
I know you'll catch this, but you may need to provide error handling in the event that a file doesn't have an extension. Not sure what happens one of these two I suspect:

1) extension is an empty string which will probably cause a runtime error or
2) you end up matching for "|" which will alway be true.

Later

Darrell
 
Top Bottom