tags:

views:

125

answers:

5

If I want to check how many files there are in a folder (where there is likely to be 10,000+ files), is there a more efficient way than...

Public Function FileCount(FolderName as String) As Integer  

   Dim Files() As String

   Files = IO.Directory.GetFiles(FolderName)

   Return Files.Length  

End Function

It seems to be unweildly to be forced to load such a potentially large array. Any better suggestions?

A: 

No I think

But you can return IO.Directory.GetFiles(FolderName).Length

Anuraj
How is that different?
SolutionYogi
No I didn't said its different, I only removed 3 lines of code to single line. Thats all.
Anuraj
You still have to store the temporary object, there is no difference
Ed Swangren
the qualms with the solution are its performance, not its code size, since the IL for the two is virtually identical.
DevelopingChris
+4  A: 

Has it caused a problem? It's not like GetFiles() is returning the contents of each file, it is returning to you their paths, i.e., a string array.

Ed Swangren
Still, 10,000 times ~12 (being optimistic, 8.3) is 120,000 bytes, which is a sizable chunk of memory to get at an integer... Still, you're probably right about premature optimisation...
Matthew Scharley
120 kilobytes...for about a split second...I wouldn't worry really.
Ed Swangren
Because each array element contains the full path as well as the filename, the average size is roughly 60 chars per file, so we're talking about creating a 600k block of memory (admittedly for only a small time period) just to get a single number. It's this sort of inefficiency generally that adds up. But... memory and electricity are cheap - for now.
Bill
+1  A: 

It may be unweildy but if you run a test and find the performance impact is minimal, it might still be the appropriate solution.

Joe
A: 

I don't think it's a problem. Under the hood the files aren't opened anyway, the function call only queries the disk file index or file allocation table for the files under that folder. It's no different than doing a dir on the command prompt.

Best way to see if it really causes a problem is to profile it.

Jon Limjap
+2  A: 

Unfortunately, you can't enumerate the contents of a directory in .NET 3.5 without using interop or obtaining all of the paths ahead of time. As mentioned, the disadvantage to using File.GetFiles() is that it returns all the paths at once, which can consume more resources than required.

If you want to enumerate the contents of the directory and increment a counter as you enumerate, you may try one of the following options.

  • Using .NET interop, call FindFirstFile and FindNextFile with the . pattern, incrementing a counter for each successful invocation.
  • Utilize features of .NET 4.0 which allow you to obtain an IEnumerable<T> for enumerating the contents of a directory.

Whenever interop is used, you should be concerned about the performance of making native WinAPI calls from managed code. Consider utilizing a profiler to determine if the performance penalty is acceptable.

Steve Guidi
Bumping this up because it's a high quality answer. GetFiles is fine for 10,000 files. You're going to cry if you're using it to count a directory containing million of files.