未正确检测文件夹(目录)

发布于 2024-08-07 21:22:51 字数 1225 浏览 0 评论 0原文

我注意到“bog standard”Objective-C 文件夹检测代码有错误。我正在扫描给定路径的文件和文件夹,并记录存在的文件数量和文件夹数量。

奇怪的是,我返回的文件夹数量比实际存在的文件夹数量多了一个!

通过调试逐步执行例程并在例程确定对象是文件还是文件夹时观察每个文件/文件夹名称,这表明其中一个文件正在通过文件夹测试!?!?!?

错误检测到的文件是“带附件的 RTF (RTFD)”文件类型。我还没有检查其他文件夹,看看是否有更多文件类型可能会错误地报告自身。

除了这个文件之外,我的程序中的其他所有内容都工作正常。

有谁知道我可能做错了什么?或者这是 Objective-C 中的一个已知错误?

这是我正在使用的代码的一部分:

 BOOL isDir;
    NSString *file;
 NSString *docsDir = [self path];
 NSFileManager *manager = [NSFileManager defaultManager];
 NSDirectoryEnumerator *dirEnum = [manager enumeratorAtPath: docsDir];
 NSDictionary *fattrs;
        //(only showing important declarations above)

while (file = [dirEnum nextObject]) {
    //If user clicked the Abort Button, get out of the loop
    if (abortFlag)
        break;

    if ([excludeSubdirectories state] == NSOnState) {
        [dirEnum skipDescendents];
    }

    if ([manager fileExistsAtPath:[docsDir stringByAppendingPathComponent:file]
                      isDirectory:&isDir] && isDir) {
       ++dirCount;
       if ([excludeSubdirectories state] == NSOnState) {
           continue;
       }
    }
}
            //... Do a bunch of other stuff, etc., etc. ...

I am noticing an error with the 'bog standard' Objective-C folder detection code. I am scanning files and folders with a given path and keeping count of how many files exist and how many folders exist.

Oddly enough, I am returning a count of one more folder than actually exists!

Stepping through the routine with debug and watching each file/folder name as the routine determines if the object is a file or folder, is showing me that one of the files is passing the test of being a folder!?!?!?

The file that is being detected incorrectly is an 'RTF with attachments (RTFD)' file type. I haven't checked other folders yet to see if there are more file types that might report themselves incorrectly.

Apart from this one file, everything else in my program is working correctly.

Does anyone have any ideas what I might be doing incorrectly? Or is it a known bug in Objective-C?

Here is part of the code I am using:

 BOOL isDir;
    NSString *file;
 NSString *docsDir = [self path];
 NSFileManager *manager = [NSFileManager defaultManager];
 NSDirectoryEnumerator *dirEnum = [manager enumeratorAtPath: docsDir];
 NSDictionary *fattrs;
        //(only showing important declarations above)

while (file = [dirEnum nextObject]) {
    //If user clicked the Abort Button, get out of the loop
    if (abortFlag)
        break;

    if ([excludeSubdirectories state] == NSOnState) {
        [dirEnum skipDescendents];
    }

    if ([manager fileExistsAtPath:[docsDir stringByAppendingPathComponent:file]
                      isDirectory:&isDir] && isDir) {
       ++dirCount;
       if ([excludeSubdirectories state] == NSOnState) {
           continue;
       }
    }
}
            //... Do a bunch of other stuff, etc., etc. ...

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

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

发布评论

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

评论(1

听不够的曲调 2024-08-14 21:22:51

RTFD 文件实际上是捆绑包,它们只是奇特的目录。您可以通过右键单击 rtfd 文件并选择“显示包内容”选项来验证这一点。

如果您在 Mac OS X 上编写此内容,则可以使用 -[NSWorkspace isFilePackageAtPath:] 来识别这些包。

RTFD files are actually bundles, which are just fancy directories. You can verify this by right-clicking on your rtfd file and choosing the "Show Package Contents" option.

If you're writing this on Mac OS X, you can use -[NSWorkspace isFilePackageAtPath:] to identify these bundles.

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