如何有效地检索目录中的文件数量?
有没有办法(在使用 Delphi 2010 的 Windows 上)获取目录中的文件数量,而无需实际遍历所有文件?
我想在某些文件系统索引操作期间实现进度条。因此,我需要知道目录中有多少文件。
获取目录中文件数量的最快方法是什么?
Is there a way (on windows using Delphi 2010) to get the number of files in a dirctory without actually traversing all files?
I want to implement a progress bar during some file system indexing operation. For that reason I need to know how many files are in the directory.
What is the fastest way to get the number of files in a directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 Windows 7 或 Server 2008 R2 上运行,我建议从 SysUtils 中提取 FindFirst 和 FindMatchingFile 函数,并破解前者以使用 FindFirstFileEx 而不是 FindFirstFile。然后,您可以暂时将附加标志参数设置为 2(在 MSDN 中定义为 FIND_FIRST_EX_LARGE_FETCH ),此设置暂时以 (Win32majorversion = 6) 和 (Win32minorversion >= 1) 为条件。
此设置可显着提高这些操作系统上的 FindFirst/FindNext 循环的速度。在 MSDN 上查找 FindFirstFileEx 了解更多详细信息,如下Delphi帮助检索到的Microsoft文档中没有最新的文档。
TDirectory.GetFiles 最终似乎会调用 FindFirst,因此除了简化您自己的代码之外,不会给您带来太多好处。
If you are running on Windows 7 or Server 2008 R2, I recommend extracting the FindFirst and FindMatchingFile functions from SysUtils and hacking the former to use FindFirstFileEx instead of FindFirstFile. Then you can set the additional flags parameter to 2 (defined in MSDN as
FIND_FIRST_EX_LARGE_FETCH
) with this setting conditioned on (Win32majorversion = 6) and (Win32minorversion >= 1), for the time being.This setting produces a very significant speed increase for FindFirst/FindNext loops on these OS. Look for FindFirstFileEx on MSDN for more details, as the latest documentation is not in the Microsoft documentation retrieved by Delphi help.
TDirectory.GetFiles eventually seems to call FindFirst, so will not buy you much advantage other than simplifying your own code.
我认为最快的方法是使用位于 IOutils.pas 中的 TDirectory.GetFiles 方法。由于每个用户的目录中(可见)文件的数量可能不同,因此只有很小的机会可以以某种方式检索到一个数字。
FindFirst/FindNext 方法(包含在上述方法中)实际上并不遍历文件,它仅遍历表中的条目,因此它可能比预期更快。
I think the fastest way is to use the TDirectory.GetFiles method located in IOutils.pas. As the number of (visible) files in a directory may be different for each user, there is only a tiny chance that there is just a number to retrieve somehow.
The FindFirst/FindNext approach (which is wrapped in the above method) doesn't actually traverse files, it only traverses entries in a table, so it might be faster than expected.