fast file count method?

  • Thread starter Thread starter Rob R. Ainscough
  • Start date Start date
R

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.
 
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
 
Luca Beretta said:
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
 
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?

Carl Daniel said:
Luca Beretta said:
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
 
Rob said:
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
 
Rob said:
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
 
Back
Top