Directory.GetFiles Filter

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
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! :)
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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

 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
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).
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
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
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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...
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
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
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
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

 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
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.
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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! :)
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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...
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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...
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
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:

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
I'm running this in a asp.net web page itself trying to view the folder/file structure of the web site
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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.
 

smithd3

Member
Joined
Jun 10, 2004
Messages
14
Programming Experience
10+
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
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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!
 

Neal

Forum Admin
Staff member
Joined
Jun 2, 2004
Messages
123
Location
VA
Programming Experience
10+
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
 
Top Bottom