Efficient way to get file count in filesystem

Priya 211 Reputation points
2021-11-15T13:26:58.257+00:00

Hello All,

Is there an efficient way to get the file count for a filesystem which has lot of small files. Have used get-childitem cmdlet, however interested to know if there any alternate method to get the same fast.

Thanks!

Windows for business Windows Server User experience Other
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Gary Reynolds 9,621 Reputation points
    2021-11-15T21:21:51.887+00:00

    Hi,

    I would make it even more simple, from the command prompt at the top of the directory you want to count the files run dir /a:-d /s and the file count is display when it finishes.

    Gary.

    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2021-11-15T21:50:15.24+00:00

    Depending on your overall goal.... I like TreeSize Free. It's multithreaded.

    https://www.jam-software.com/treesize_free

    I use the GUI, but it appears capable of generating reports, if that's what you're looking for.

    https://knowledgebase.jam-software.com/5859

    The Pro version has more features.

    0 comments No comments

  3. Priya 211 Reputation points
    2021-11-16T05:48:13.647+00:00

    Thanks everyone for the quick responses.

    Basically I am looking for a mechanism which would take less time to get the filecount recursively. Will compare the results with both get-childitem and dir options.

    0 comments No comments

  4. Gary Nebbett 6,216 Reputation points
    2021-11-16T10:00:32+00:00

    Hello @Priya ,

    Let's first consider how one can count files.

    One way is to traverse the tree/network of files via recursive directory enumeration. Some features of file systems (such as hard links and reparse points) need to be considered and handled (e.g. recognizing hard links, (not) following reparse points).

    All recursive directory enumeration implementations ultimately use the NtQueryInformationFile/ZwQueryInformationFile routine. One parameter of this routine is a FILE_INFORMATION_CLASS and there are several values that could be used (reporting different levels of information) - choosing the value that reports just enough information to count files (and detect hard links, reparse points, etc.) could save a small amount of time.

    Another option is to enumerate the on-disk data structures that implement a file system. Assuming that "file system" means "volume", one could use FSCTL_GET_NTFS_* ioctls to enumerate the file records on an NTFS volume. A small amount of "well known but not officially documented" information might be needed to distinguish between plain files and directories.

    Yet another option is to count the total number of files once and then use change tracking mechanisms to calculate the delta values for subsequent requests.

    Gary

    0 comments No comments

  5. Priya 211 Reputation points
    2021-11-16T10:09:40.997+00:00

    Sure. Thank you Gary.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.