Directory.GetFiles Filter

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
132
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! :)
 
I'm running this in a asp.net web page itself trying to view the folder/file structure of the web site
 
The only problem so far is .js is getting picked up because of .jsp is an extension. Need to ensure the entire extension is compared, not "part" of it.
 
Neal,

Apply a delimiter character "|" to the end of the extension. That should take care of that issue. Do you have it displaying the .aspx files now? I modified the imagebrowser for bv and it displays those files just fine on my test box.

Darrell
 
Yes, I had to do a recursion system as I was missing a few folders. User error! :) I reverted it to the EndsWith, but I'll change it per your suggestion and give that a whirl! Thanks!
 
smithd3 said:
Neal,

Apply a delimiter character "|" to the end of the extension. That should take care of that issue. Do you have it displaying the .aspx files now? I modified the imagebrowser for bv and it displays those files just fine on my test box.

Darrell
What is your suggested code change for this? I'm not following
 
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 :)
 
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! :)
 
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
 
Back
Top