Home All Groups Group Topic Archive Search About

fast file count method?

Author
21 Jun 2006 4:24 PM
Rob R. Ainscough
Is there some way to determine file count on a drive?  I know I can use
System.IO.DriveInfo and My.Computer.FileSystem.GetFiles to obtain a file
count, but this approach is pretty slow -- is there a faster more efficient
method available?

..NET 2.0 framework is my base.

thanks, Rob.

Author
21 Jun 2006 7:46 PM
Luca Beretta
the managed way is :

System.IO.DirectoryInfo dinfo = new System.IO.DirectoryInfo("c:\\windows");
int filecount = dinfo.GetFiles().Length;

otherwise you need to PInvoke C++ function from Win API.

HTH

Show quote
"Rob R. Ainscough" wrote:

> Is there some way to determine file count on a drive?  I know I can use
> System.IO.DriveInfo and My.Computer.FileSystem.GetFiles to obtain a file
> count, but this approach is pretty slow -- is there a faster more efficient
> method available?
>
> ..NET 2.0 framework is my base.
>
> thanks, Rob.
>
>
>
Author
21 Jun 2006 11:17 PM
Carl Daniel [VC++ MVP]
"Luca Beretta" <LucaBere***@discussions.microsoft.com> wrote in message
news:C834CC43-5653-45F3-AC92-C08A7A46C672@microsoft.com...
> the managed way is :
>
> System.IO.DirectoryInfo dinfo = new
> System.IO.DirectoryInfo("c:\\windows");
> int filecount = dinfo.GetFiles().Length;
>
> otherwise you need to PInvoke C++ function from Win API.

.... which will be just as slow as the method the OP suggested.

In short, no - there's no faster way to get a full count of files than by
counting them.  Exactly how you go about counting them is a matter of some
debate, but they'll all boil down to calls to FindFirstFile/FindNextFile at
the Win32 level and so will all take about the same amount of time.

You can use the class described in this article:

http://www.codeproject.com/cs/files/FileSystemEnumerator.asp

to count files as well - it will take the same amount of time as the methods
described above, but it won't take as much memory since it doesn't return a
string[] of file names.

-cd
Author
22 Jun 2006 8:26 PM
Rob R. Ainscough
There has to be a more efficient approach?  Especially since windows index
services are running on most PCs.  Is the OS really that handicapped such
that it doesn't keep a pointer count?

Show quote
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:eECZ$hYlGHA.5044@TK2MSFTNGP02.phx.gbl...
> "Luca Beretta" <LucaBere***@discussions.microsoft.com> wrote in message
> news:C834CC43-5653-45F3-AC92-C08A7A46C672@microsoft.com...
>> the managed way is :
>>
>> System.IO.DirectoryInfo dinfo = new
>> System.IO.DirectoryInfo("c:\\windows");
>> int filecount = dinfo.GetFiles().Length;
>>
>> otherwise you need to PInvoke C++ function from Win API.
>
> ... which will be just as slow as the method the OP suggested.
>
> In short, no - there's no faster way to get a full count of files than by
> counting them.  Exactly how you go about counting them is a matter of some
> debate, but they'll all boil down to calls to FindFirstFile/FindNextFile
> at the Win32 level and so will all take about the same amount of time.
>
> You can use the class described in this article:
>
> http://www.codeproject.com/cs/files/FileSystemEnumerator.asp
>
> to count files as well - it will take the same amount of time as the
> methods described above, but it won't take as much memory since it doesn't
> return a string[] of file names.
>
> -cd
>
>
Author
23 Jun 2006 7:06 AM
Damien
Rob R. Ainscough wrote:
> There has to be a more efficient approach?  Especially since windows index
> services are running on most PCs.  Is the OS really that handicapped such
> that it doesn't keep a pointer count?
>
>From some of what you posted, I gather you're trying to count the
number of files on a disk, rather than just the number within a
particular directory?

If this is the case, then go into the root of your system drive,
highlight everything, and bring up the properties. Notice how the count
of files is goes up, as the system traverses all of the directories and
does file counts? If something as simple as the properties page is
having to do this work, it pretty much indicates to me that there isn't
a faster method available in Windows, or they'd have done it there
already.

Damien
Author
23 Jun 2006 1:19 PM
Carl Daniel [VC++ MVP]
Rob R. Ainscough wrote:
> There has to be a more efficient approach?  Especially since windows
> index services are running on most PCs.  Is the OS really that
> handicapped such that it doesn't keep a pointer count?

I don't know if I'd call it handicapped, but yes - there's no documented
better way.  The total file count simply isn't kept by the file system.  In
the metadata of NTFS there are counts of useful things (useful to the
filesystem) like the MFT size, and the number of used MFT records, but the
total file count is apparently not useful enough to be worth the effort of
maintaining it.

I suspect that a low-level scan of the MFT to count file records would be
more efficient, but there's no documented APIs for doing that.

-cd

AddThis Social Bookmark Button