有效列出目录中的所有子目录 [英] Efficiently List All Sub-Directories in a Directory

查看:108
本文介绍了有效列出目录中的所有子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图列出所有的目录(文件夹)。

在一个给定的目录中使用WinAPI& C ++。



现在我的算法很慢&低效:

- 使用FindFirstFileEx()打开我正在搜索的文件夹

- 然后查看目录中的每个文件(使用FindNextFile());如果它的目录文件,然后我将其绝对路径存储在一个向量,如果它只是一个文件,我什么都不做。



这看起来非常低效,因为我正在查看目录中的每个文件。




  • 是否有一个可以使用的WinAPI函数,它会告诉我给定目录中的所有子目录?
  • 你知道一个算法,我可以用来有效地定位&识别目录(文件夹)中的文件夹?


编辑:在接受了我使用FindExSearchLimitToDirectories搜索的建议之后,我仍然会打印出所有文件(.txt等)&不只是文件夹。我做错了什么?

  WIN32_FIND_DATA dirData; 
HANDLE dir = FindFirstFileEx(c:/ users / soribo / desktop \\ *,FindExInfoStandard,& dirData,
FindExSearchLimitToDirectories,NULL,0); (FindNextFile(dir,& dirData)!= 0)


printf(FileName:%s \ n,dirData.cFileName);


解决方案

必须在文件系统级别提供支持。如果这不存在,则系统必须枚举目录中的每个对象。



原则上,您可以使用 FindFirstFileEx 指定 FindExSearchLimitToDirectories 标志。但是,文档状态(重点是我的):


这是一个咨询标志。如果文件系统支持目录过滤,则函数将搜索与指定名称匹配的文件,并且该目录也是一个目录。如果文件系统不支持目录过滤,则忽略该标志。



如果需要进行目录过滤,则可以在所有文件系统上使用此标志,但是因为它是一个咨询标志,只影响支持它的文件系统。应用程序必须检查存储在FindFirstFileEx函数的lpFindFileData参数中的文件属性数据,以确定函数是否已经返回到目录的句柄

然而,据我所知,信息稀少, FindExSearchLimitToDirectories flag在桌面文件系统上并没有得到广泛的支持。

最好的办法是使用 FindFirstFileEx FindExSearchLimitToDirectories 。如果遇到文件系统级别不支持目录过滤的文件系统,则仍然必须执行自己的过滤。如果你幸运的话,打到一个支持它的文件系统,那么你将获得性能优势。


Please see edit with advice taken so far...

I am attempting to list all the directories(folders) in a given directory using WinAPI & C++.

Right now my algorithm is slow & inefficient:
- Use FindFirstFileEx() to open the folder I am searching
- I then look at every file in the directory(using FindNextFile()); if its a directory file then I store its absolute path in a vector, if its just a file I do nothing.

This seems extremely inefficient because I am looking at every file in the directory.

  • Is there a WinAPI function that I can use that will tell me all the sub-directories in a given directory?
  • Do you know of an algorithm I could use to efficiently locate & identify folders in a directory(folder)?

EDIT: So after taking the advice I have searched using FindExSearchLimitToDirectories but for me it still prints out all the files(.txt, etc.) & not just folders. Am I doing something wrong?

WIN32_FIND_DATA dirData;
HANDLE dir = FindFirstFileEx( "c:/users/soribo/desktop\\*", FindExInfoStandard, &dirData, 
                              FindExSearchLimitToDirectories, NULL, 0 );

while ( FindNextFile( dir, &dirData ) != 0 )
{
    printf( "FileName: %s\n", dirData.cFileName );
}

解决方案

In order to see a performance boost there must be support at the file system level. If this does not exist then the system must enumerate every single object in the directory.

In principle, you can use FindFirstFileEx specifying the FindExSearchLimitToDirectories flag. However, the documentation states (emphasis mine):

This is an advisory flag. If the file system supports directory filtering, the function searches for a file that matches the specified name and is also a directory. If the file system does not support directory filtering, this flag is silently ignored.

If directory filtering is desired, this flag can be used on all file systems, but because it is an advisory flag and only affects file systems that support it, the application must examine the file attribute data stored in the lpFindFileData parameter of the FindFirstFileEx function to determine whether the function has returned a handle to a directory.

However, from what I can tell, and information is sparse, FindExSearchLimitToDirectories flag is not widely supported on desktop file systems.

Your best bet is to use FindFirstFileEx with FindExSearchLimitToDirectories. You must still perform your own filtering in case you meet a file system that doesn't support directory filtering at file system level. If you get lucky and hit upon a file system that does support it then you will get the performance benefit.

这篇关于有效列出目录中的所有子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆