1

I've got a problem counting the files on a fileshare (which are present longer than 1 hour):

The two 'known' methods:

GetFiles

dir.Getfiles.Where(Function(x) x.CreationTime < DateTime.Now.AddHours(-1)).Count()

Link: GetFiles

EnumerateFiles

dir.EnumerateFiles.Where(Function(x) x.CreationTime < DateTime.Now.AddHours(-1)).Count()

Link: EnumerateFiles

Is there any faster method to count the number of files inside a folder/fileshare?

The number of files may vary from 2000 to upwards of 500 000. Both methods shown above show a drastic drop in performance beyond the 30 000 files.

Questions found on SO that didn't resolve it for me:

fastest-way-to-count-folder-files-in-net-4-0

how-to-correctly-count-the-number-of-files-in-a-folder

Community
  • 1
  • 1
User999999
  • 2,500
  • 7
  • 37
  • 63
  • 2
    Do you have 500,000 files in one directory? Maybe that is the problem you should try and solve first. – Matt Wilko Mar 13 '14 at 08:55
  • There is no way to work around that atm. we're are working in a productionenvironement and costs would skyrocket. We were hoping to use the above count to monitor the different amounts of files in order to create a graph overtime. Our ultimate goal is ofcourse to split the different businessprocesses, but without actual grounds/proof we can't ask the management to invest money. – User999999 Mar 13 '14 at 09:09

1 Answers1

1

Another option is:

FileSystemWatcher Class

Track add and delete.
Add and remove from a collection.

I would assume DirectoryInfo.EnumerateFiles would be faster.
For two reasons:
One it enumerates before the whole collection returns
Two it includes date. Getfiles only returns the name so it may be making a second trip to get the date.

DirectoryInfo Methods

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Just what i needed! I can get the original amount of files using `EnumerateFiles`. After that i can monitor the changes using FileSystemWatcher(using `created` and `deleted`). Just did a test on a testdirectory and works a lot better then my original solution. – User999999 Mar 13 '14 at 14:52
  • What kind of collection are you using and what are you storing in the collection? – paparazzo Mar 13 '14 at 14:58
  • Actually i'm using the `Count()` and storing it in an Integer. This integer is used in our monitoringtool. The content of the file is of no use for us. the row of integers are shown as graph. A steep incline points to issues on the specified systems. The normal avearage files in queue is 12000 if it exceeds the 90 000 there is likely an issue with the specified system. (the system can handle up to 30 000 files an hour) – User999999 Mar 13 '14 at 15:10
  • OK, you don't need to explain. But if you are just using a count you don't know how many are older than one hour. But that makes it a lot easier and faster. – paparazzo Mar 13 '14 at 19:44