Updating the progress bar while searching for a file

This question provides a quick way to use kernel.dll to recursively search for file attributes, for example. file names. The problem is that the progress report (for example, in the Windows Forms application) is limited to the file or directory in which it is currently located, because it does not have information about the total number of files.

Although I know in Windows 7, if you use a file explorer to search for a file, it shows a progress bar:

enter image description here

So how do they do it? Is the total number of files known here ahead of time? Is it possible to simulate such a progress report in the answer from the question related above? I am not sure how to do this without the total number of files.

The closest question I could find is this one that seems to have some problems with this recursion method, since I don't have a folder index in advance, and the behavior is very strange for one directory of many files.

+4
source share
2 answers

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.;)

+5
source

Run a stream in the background that counts the files in the directory you are looking for. In the background, update the number of files counted so far. Use this score in your progress bar. When this number is more than one, it is safe to start the search. When calculating the progress, pull out the result if your search (miraculously) is ahead of your background file counter or if a noticeable deviation in the progress indicator is unacceptable.

In this case, the fastest way to count files is found out (consider FindFirstFileEx). On a local drive, it may take 2 or 3 seconds to count a folder, such as Program Files. This will take a lot longer over the network, since with FindFirstFileEx file names are transferred when you only need the number of files and the directory list.

All of this suggests that you spend much more time searching than simply counting files.

+1
source

All Articles