问题描述
这个 似乎有这种递归方法存在一些问题,因为我没有预先计算文件夹数量,而且对于包含许多文件的单个目录来说,这种行为会非常奇怪.
根据您需要获得的准确度,可能有一个简单的两遍解决方案(对于网络驱动器不是最佳的,因此您可能需要在那里进行调整).
对于目录的前 n
级(比如 4,包括驱动器),计算子目录的数量.这通常是一个快速操作,尽管您可以将其调整为仅在存在超过 5 个子目录或类似的子目录时才递归.存储此号码.
当您执行真正的搜索时,请跟踪您在根目录的 n
步内完成的子目录的数量.使用此数字和存储的计数来估计完成情况.
以基本结构为例:
<代码>C:一个1一世二三23乙C
计数a,1,忽略i和兄弟,计数2等.然后,一边搜索, 完成搜索3、2、1、a等时增加条形
现在,这绝对不是万无一失的.可能存在竞争条件,它不是非常准确,以及各种各样的其他事情.
但是,对于低粒度的进度条,它足够接近以至于看起来非常准确.更重要的是,从用户体验的角度来看,使用存储的计数并将进度与之进行比较往往会阻止进度条在过程中途增长.
我实际上在一些代码中使用了这种技术这里.
初始构建(下降了 10 个级别)仍然非常快.我不记得它进行了多少测试,但是在搜索 2.5-3 百万个文件时,该栏明显准确,没有很多停顿(尽管只提前检查了 1/1000).请注意,进度条越短,显示的越准确.;)
This question provides a fast way to use the kernel.dll
to recursively find file attributes, e.g. file names. The problem is reporting progress (such as in a Windows Forms App) is limited to which file or directory it is currently on as it has no information as to total file count upfront.
Though, I know in Windows 7 if you use the file explorer to search for a file, it shows a progress bar for the search:
So how is it they do it here? Is total file count known here ahead of time? Is it possible to mimic this kind of progress reporting in the answer from the question linked above? I'm not sure how to do it without a total file count upfront.
The closest question I could find was this one which seems to have some problems with this recursion method as I don't have folder count upfront, and the behavior would be very odd for a single directory of many files.
Depending on how accurate you need to get, there may be a simple two-pass solution (not optimal for network drives, so you may need to tune there).
For the first n
levels of directories (say 4, including drive), count the number of sub-directories. This is typically a quick operation, although you can tweak it to only recurse when more than say 5 subdirectories are present or similar. Store this number.
As you perform the real search, keep track of the number of subdirectories you've completed that are within n
steps of the root. Use this number and the stored count to estimate completion.
For example, with the basic structure:
C:
a
1
i
ii
iii
2
3
b
c
Count a, 1, ignore i and siblings, count 2, etc. Then, while searching, increase the bar when you finish searching 3, 2, 1, a, etc.
Now, this is absolutely not fool-proof. There may be race conditions, it's not terribly accurate, and all sorts of other things.
However, for a low-granularity progress bar, it's close enough that it will appear pretty accurate. More importantly from a user-experience perspective, using a stored count and comparing progress against that tends to prevent the bar from growing halfway through the process.
I'm actually using this technique in some code here.
The initial build, which goes down 10 levels, is still pretty speedy. I don't remember just how much testing went into it, but the bar is visibly accurate without many pauses when searching through 2.5-3 million files (despite only checking 1/1000th of that ahead of time). Note that the shorter your progress bar, the more accurate it will appear. ;)
这篇关于在文件搜索期间更新进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!