Violet Giraffe
Violet Giraffe

Reputation: 33599

Is there any faster way to get a directory size in Windows than iterate over all its children?

My program needs to get the dir size with all subdirs and subfiles, and it does that by recursively enumerating all the objects in that dir and summing up the size. However, performace is unacceptable for large directories. It is longer than Windows Explorer directory Properties calculates size, and it causes the hard drive to rattle much more. So, how can I optimize this process? Is there any appropriate WinAPI function or method for this?

Upvotes: 0

Views: 1501

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129374

Iterating over the files in the directory is the only generic way, and Windows certainly doesn't have any short-cut for doing this (in regular applications that run with user-level privileges - and I wouldn't suggest that your app should require admin rights just to run!).

There MAY be a bit of a difference, if the directory contains a very large number of files, depending on whether you do depth first or breadth first recursion of the directories - the breadth first would require "saving" the directories to be searched within the current directory, which could of course also lead to problems if you have many directories, where the depth-first method doesn't need any storage, but means that the OS will have more directories open at once - and probably make more head movements. However, it is LIKELY very much a marginal difference. For large filesystems, "how much space is used" it could make a difference - I haven't actually tried.

Upvotes: 1

Related Questions