有没有办法知道一个目录是否包含子目录?

发布于 2024-07-26 10:08:51 字数 188 浏览 3 评论 0原文

我正在 C++ 中工作。 有没有办法知道一个目录是否包含子目录?

CFileFind 似乎必须搜索所有文件。 如果唯一的子目录位于列表末尾并且文件很多,则非常耗时。

例如:目录A包含99995个文件和FindNextFile列表末尾的一个子目录。 我是否尝试了 99995 次,然后说:是的,它包含子目录?

I am woking in c++.
Is there any method to know whether a directory contain a sub directory?

CFileFind seems have to search through total files.
It is time consuming if the only subdirectory is at the end of the list and the there are lots of files.

for example: directory A contains 99995 files and one subdirectory at the end of FindNextFile List. had I try 99995 times, then say: yes, it contains subdirectory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫轩蝶泪 2024-08-02 10:08:51

来自 Microsoft 的 Raymond Chen 写了一篇文章,可能适用于此:计算目录的大小不仅仅是添加文件大小。 本质上,他解释说,像目录大小这样的信息不能存储在目录的条目中,因为不同的用户可能具有不同的权限,可能会使某些文件对他们不可见。 因此,获得用户应该看到的大小的唯一方法是根据用户的请求进行计算。

就您而言,答案可能源于相同的推理。 仅当您的应用程序要求时,才能确定您的应用程序可用的目录列表,因为它的根目录视图可能与其他应用程序的不同,使用不同的凭据运行。 我不知道为什么 Windows 将目录与文件一起存储,但这是既定的。

由于 Win32 与您在用户模式下访问文件系统的距离最接近,因此我会避免使用任何更高级别的解决方案,例如 .NET,因为它可能只会简化界面。 司机可能工作得更快,但这超出了我的知识范围。

Raymond Chen from Microsoft has written a post that probably applies here: Computing the size of a directory is more than just adding file sizes. In essence, he explains that information like the size of a dir cannot be stored in the dir's entry, because different users might have different permissions, possibly making some of the files invisible to them. Therefore, the only way to get the size the user should see is to calculate it upon request from the user.

In your case, the answer probably stems from the same reasoning. The list of directories available to your app can only be determined when your app asks for it, as its view of the root directory might be different than another app's, running with different credentials. Why Windows store directories along with files I don't know, but that's a given.

Since Win32 is as close as you'll get to the file system in user mode, I'd avoid any higher level solutions such as .NET, as it might only simplify the interface. A driver might work quicker, but that out of the scope of my knowledge.

海的爱人是光 2024-08-02 10:08:51

如果您使用 .Net 框架,您可以使用 Directory.GetDirectories并检查数组的大小是否为 0。不知道这是否会给你带来速度。

如果您可以控制目录,则可以应用命名约定,以便具有子目录的目录以一种方式命名,而没有子目录的目录以另一种方式命名。

If you are using the .Net framework you could use Directory.GetDirectories and check is the size of the array is 0. Do not know how if this will give you speed.

If you have control over the directories you could apply a naming convention so that directories that have sub directories are named one way and directories with out sub directories are named another.

梦中楼上月下 2024-08-02 10:08:51

您可以尝试使用 boost 文件系统库。

名为directory_iterator的类[在boost/filesystem/operations.hpp中声明]具有许多可用于列出文件、查找文件是否是子目录的函数(is_directory——我想这就是您正在寻找的)等等..

请参阅以下链接了解更多信息。
链接文本

看来您正在使用 MFC [刚刚看到您正在使用 CFileFind ],之前没有看到。
抱歉,没有太多信息。 您可能必须使用 FindFirstFile/FindNextFile。

You can try using the boost filesystem library.

A class by name directory_iterator [ declared in boost/filesystem/operations.hpp ] has many functions which can be used for listing files, finding whether the file is a sub-directory ( is_directory -- I guess this is what you are looking for ) etc..

Refer the following link for more information.
link text

It seems you are using MFC [ just saw that you are using CFileFind ], didn't see that earlier.
Sorry, Didn't have much info. You may have to use FindFirstFile/FindNextFile.

潜移默化 2024-08-02 10:08:51

这是否可以很快完成完全取决于平台。

在 Win32 上,您可以使用 FindFirstFile/FindNextFile 或 MFC CFileFind 等包装器,它们按某种顺序列出项目,而不能强制首先列出目录。

Whether this can be done very fast is entirely platform-dependent.

On Win32 you use FindFirstFile/FindNextFile or wrappers on top of those like MFC CFileFind and they list items in some order that can't be forced to list directories first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文