Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have previously asked about searching for multiple file types. The solution that was given suggests that I add a new for each statement for every file extention type that I wish to search for and then to return the value to getfiles. My problem is that I have 40 or 45 file types I wish to search for so adding 40 or 45 for each statements will have a big performance issue.

Am I able to use a select case statement to add all the file extensions to? and then will i be able to somehow call the case? is that how it works?


Thank you in advance you guys rock!
Posted

No. There are no cases to put in select case statement. And big number of file extensions (40-45?) is not a big factor of performance at all compared to the total number of files found assuming this number is considerable.

This number of different file extension is not so big to be relevant to performance at all; the only real factor is a total number of file names found. Let's say, you will make a double loop: outer loop will run through a set of extensions you need (for example, an array of strings each representing a file mask) and call to System.IO.DirectoryGetFiles(String, String, SearchOption), and internal loop will run through all the files found for each file mask. See http://msdn.microsoft.com/en-us/library/system.io.directory.aspx[^].

Don't forget, there is a caveat in using a file mask. It is described with the solution here: Directory.Get.Files search pattern problem[^].

—SA
 
Share this answer
 
v2
Comments
Prerak Patel 14-Nov-11 22:56pm    
Considering that GetFiles takes a little more to execute, if directory contains only files with those required entensions (or may be very few unnecessary files) then IMO OP could use the search with *.* and use Select Case.
Sergey Alexandrovich Kryukov 16-Nov-11 12:19pm    
It depends, in fact.
--SA
You can do something like this, but it's performance also depends on number of useless files in that directory which are not to be considered based on extension.
VB
Dim filePaths() As String = System.IO.Directory.GetFiles("c:\MyDir\", "*.*", SearchOption.AllDirectories)
For Each filepath As String In filePaths
  Select Case IO.Path.GetExtension(filepath).ToLower
    Case "bmp"
    Case "jpg"
      '...
    Case Else
  End Select
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900