Depending on how accurately you need to get, there may be a simple two-pass solution (not optimal for network drives, so you may need to configure it).
For the first n directory levels (say 4, including disk), count the number of subdirectories. This is usually a quick operation, although you can set it up only for recursion if more than 5 subdirectories are present or similar. Save this number.
When you perform a real search, keep track of the number of subdirectories executed that are within n root steps. Use this number and accumulated counter to evaluate completion.
For example, with a basic structure:
C:\ a\ 1\ i\ ii\ iii\ 2\ 3\ b\ c\
Count a, 1, ignore me and brothers and sisters, count 2, etc. Then, during the search, increase the bar when you finish searching 3, 2, 1, a, etc.
Now this is absolutely not perfect. There may be race conditions, this is not very accurate, and all sorts of other things.
However, for an indicator with a low degree of detail, it is close enough to make it look pretty accurate. More importantly, from the point of view of the user, using the accumulated account and comparing progress with it usually prevents the panel from growing halfway through the process.
I use this technique in some code here .
The initial build, which goes on 10 levels, is still pretty fast. I donβt remember how many tests passed in it, but the panel is clearly accurate without many pauses when searching through 2.5-3 million files (despite the fact that it only checks the 1 / 1000th of them before). Please note that the shorter your progress bar, the more accurate it will appear.;)