I am looking for a fast way of counting the total number of files - and directories - on any NTFS volume. (In an ad-hoc way. That is, given any random box, not a specially prepared volume.)

Note: This is not about files per directory, just the overall amount of files on the volume.

Currently, the only way I know is to open the root folder of a drive in Windows Explorer select all elements, right-click to choose Properties and then wait (and wait) until explorer has counted all elements.

Is there a better/faster way?

link|improve this question

48% accept rate
feedback

3 Answers

up vote 6 down vote accepted

At the DOS prompt, type the following command:

  • dir \ /s /a /w

(The "/s" switch enables a recursive search on all sub-directories {with most Unix utilities this is usually the "-R" switch}, the "/a" switch counts all files regardless of Attributes, and the "/w" displays multiple entries on a single line so that the report will finish a little bit faster. Change "\" to the desired path you wish to begin from; for a different drive letter, such as drive D:, change it to "D:\" to search that drive.)

Once finished, you'll be returned the prompt, and then the last two lines of output will reveal a count of the total number of files and total number of directories. If you're looking for total number of filename entries, then just add those two number together, otherwise the total number of files is all you'll need.

link|improve this answer
Thanks! This worked very nicely and seems a lot better than the flaky explorer dialog. Also, it seemed quite fast (enough). – Martin Apr 20 '11 at 10:09
@Martin: A tool that I find particularly useful (and a major time saver for file system administration), which I suspect you probably will too, is the free and open source FAR Manager, which was heavily inspired by Norton Commander for DOS. It's a native 32-bit/64-bit Windows application that actually uses text mode (no graphics), and has some features in it that will also count files and directories (press CTRL-Q to count totals for the highlighted/selected files/directories): farmanager.com – Randolf Richardson Apr 21 '11 at 1:57
@Randolf: Awesome! :-D Fond memories of "better" days. I'll have to try that with PsExec! – Martin Apr 21 '11 at 6:38
feedback

If you just want an upper bound (approximation):

Type in fsutil fsinfo ntfsinfo C: in the command prompt, read the Mft Valid Data Length value, and divide it by 1024. This number could possibly be way too big, but it can't be too small.

If you need an exact value, there are faster ways but you'd need to program something (I don't know of any programs out there that precisely do this)... you'd need to read the $MFT file and parse it by hand, then figure out which ones are file entries and which ones are non-file entries... it's dramatically faster than Windows's "top-down" approach (because building the hierarchy bottom-up uses only the MFT and nothing else), but it's in no way easy.

If you're a programmer but you want a less painful (although slower) way, you could also just write a program that calls NtQueryDirectoryFile to traverse the folders instead of the default FindFirstFile/FindNextFile functions... it can be a lot faster but a bit more tricky.


Just be aware that the notion of a "file" itself is actually quite tricky. It's quite possible (and Windows even does this by default) to have multiple hardlinks to the same file, and both of them are just as "real" as any other file... do you count them once or twice?

Or you can have junctions or symbolic links that point to other places... should those be counted or no?

It's not a clear-cut process as it might seem at first, so be aware of that.

Hope that helped..


Edit

You could run

 robocopy /L /E C:\ C:\Temp > "%Temp%\Temp.log"

and then inspect the "Files" statistic that's shown. :P

link|improve this answer
Re "notion of a \"file\"": Technically, directories are files too. – grawity Apr 20 '11 at 14:26
feedback

The simple answer is; download Everything. It counts for you, super fast. Also does ~realtime searches for you. It is freeware and does not contain any extra crapola.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.