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! :)
 
Well, I think the result is the same, however you have to take an additional trip to fill a DS then filter it. The filter would be the same. I'm just using:

If fileInfo.Name.EndsWith(".asp") Or fileInfo.Name.EndsWith(".aspx") Or fileInfo.Name.EndsWith(".htm") Or fileInfo.Name.EndsWith(".html") Or fileInfo.Name.EndsWith(".zip") Or fileInfo.Name.EndsWith(".exe") Or fileInfo.Name.EndsWith(".shtml") Or fileInfo.Name.EndsWith(".php") Or fileInfo.Name.EndsWith(".cfm") Or fileInfo.Name.EndsWith(".cgi") Or fileInfo.Name.EndsWith(".jsp") Or fileInfo.Name.EndsWith(".php3") Or fileInfo.Name.EndsWith(".phtml") Or fileInfo.Name.EndsWith(".pl") Or fileInfo.Name.EndsWith(".shtm") Or fileInfo.Name.EndsWith(".xhtml") Or fileInfo.Name.EndsWith(".xml") Or fileInfo.Name.EndsWith(".asf") Or fileInfo.Name.EndsWith(".avi") Or fileInfo.Name.EndsWith(".mov") Or fileInfo.Name.EndsWith(".movie") Or fileInfo.Name.EndsWith(".mp3") Or fileInfo.Name.EndsWith(".mpeg") Or fileInfo.Name.EndsWith(".mpg") Or fileInfo.Name.EndsWith(".msi") Or fileInfo.Name.EndsWith(".msp") Or fileInfo.Name.EndsWith(".wmv") Or fileInfo.Name.EndsWith(".pdf") Then

 
I've seen this question before and the answer was that multiple extensions aren't supported as a Filter for the GetFiles method, you have to make multiple calls (as you are doing).
 
Neal,

Not sure if this is any faster, but it might be worth a shot with a stop watch. I expect that using this method part of the overhead may be performed at the OS level rather then the .net runtime.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim d As New DirectoryInfo("C:\inetpub\wwwroot\bvc29")
Dim a(2) As String
Dim f As FileInfo

a(0) = "*.vb"
a(1) = "*.aspx"
For i As Integer = a.GetLowerBound(0) To a.GetUpperBound(0) - 1
For Each f In d.GetFiles(a(i))
Debug.WriteLine(f.FullName)
Next
Next
End Sub
 
Yeah, I thought about that too, however, if you're looping a lot of folders/files, I can't see it being more efficient repeating loops 38 times (whatever the number is) vice doing the loop once then filtering with the EndsWith...
 
neal007 said:
Yeah, I thought about that too, however, if you're looping a lot of folders/files, I can't see it being more efficient repeating loops 38 times (whatever the number is) vice doing the loop once then filtering with the EndsWith...
My last attempt. I promise.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim dir As New DirectoryInfo("C:\inetpub\wwwroot\bvc29")
Dim fil As FileInfo
Dim ValidExtensions As String = ".asp|.aspx|.htm|.html|.zip|.exe|.shtml|.php|" _
& ".cfm|.cgi|.jsp|.php3|.phtml|.pl|.shtm|.xhtml|.xml|.asf|.avi|" _
& ".mov|.movie|.mp3|.mpeg|.mpg|.msi|.msp|.wmv|.pdf|"

For Each fil In dir.GetFiles()
With fil.FullName
If ValidExtensions.IndexOf(.Substring(.LastIndexOf("."))) > -1 Then
Debug.WriteLine(fil.FullName)
End If
End With
Next
End Sub
 
Thanks, but this is better.

For Each fil In dir.GetFiles()

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

Debug.WriteLine(fil.FullName)

End If

Next

 
A slightly faster option, probably. Would be to include the extensions you do not want to copy in the validextensions string and use = -1 to determine which files not to include. Reverse logic, so to speak.
 
The problem is, I don't know all the "trash" that may be in a persons folder, but I do know what IS valid, therefore taking the "pessimistic" approach I s'pose you could call it! :)
 
Oddly, I can get this to work on my local system, but when on my production server, I'm not getting any results in the Folder/File cataloging. Are special permissions required to view files such as .aspx, or to list Folder/File contents?

ASPNET User, IIS_WPG, and IUSR all have READ permissions, if not higher (modify) for this test project...
 
hold that thought...I pulled the extension validation and now I'm getting results...I can see image files but not .aspx. I wonder if they are protected from viewing...
 
another thought

Neal,

I afraid that I'm not going to be much help in the permissions category. I too new to the asp and IIS side of things. How are you connecting? I usually try to connect using file shareing and am requested to provide a user id and password to gain access to list the files.

Is directory browsing enabled?

Darrell
 
Last edited:
Back
Top