在Cocoa NSFIleManager的文件夹中忽略.DS_Store和Icon文件 [英] Ignore .DS_Store and Icon files in a folder with Cocoa NSFIleManager

查看:850
本文介绍了在Cocoa NSFIleManager的文件夹中忽略.DS_Store和Icon文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用NSFileManager从目录中删除特定的文件。我想忽略隐藏的.DS_Store和图标文件(我检查的文件夹必须有一个自定义图标)在目录中,但我不小心删除它们。现在,我做以下:

I'm trying to remove specific files from a directory using NSFileManager. I would like to ignore the hidden .DS_Store and Icon files (the folder that I'm checking has to have a custom icon) that are in the directory, however I keep accidentally deleting them as well. Right now, I'm doing the following:

 NSFileManager *manager = [NSFileManager defaultManager];
 NSArray *dirContents = [manager contentsOfDirectoryAtPath:[selectedFolder stringValue] error:nil]; 
 for (int i = 0; i < [dirContents count]; i++)
 {
     NSString *theFile = [dirContents objectAtIndex:i];

     if([theFile isEqualToString:@".DS_Store"] || [theFile isEqualToString:@"Icon?"] || [theFile isEqualToString:@"Icon"])
     { 
        continue;
     }
     //do manipulations on files here
 }
[manager release];

但是.DS_Store和Icon文件在我的if语句中没有匹配。此外,当我在Finder中显示隐藏文件时,图标文件称为图标。但是,在终端的该目录中执行ls会打印出Icon?。

However, the .DS_Store and Icon files aren't being matched in my if statement. Additionally, when I show hidden files in Finder, the icon file is called "Icon". However, doing an ls in that directory in terminal prints out "Icon?".

如何正确地在我的代码中解析这些文件?

How can properly I parse these files out in my code?

感谢

EDIT:
因此,它实际上是成功忽略.DS_Store文件,

So it actually is successfully ignoring the .DS_Store file, but the Icon file is still getting past the if statement.

推荐答案

有趣的是,我相信问题 最近发布的另一个问题的一部分你的。如果您使用:

Interestingly, I believe that the question part of another question posted recently essentially answers yours. If you use:

-[NSFileManager contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:] 

doc link ),您可以传递一个选项 NSDirectoryEnumerationSkipsHiddenFiles ,以忽略隐藏文件,以便您不必检查特定的文件:

(doc link), you can pass an option, NSDirectoryEnumerationSkipsHiddenFiles, to ignore hidden files so that you don't have to check for specific ones:

NSURL * selectedFolderURL = [NSURL fileURLWithPath:[selectedFolder stringValue]];
[myFileManager contentsOfDirectoryAtURL:selectedFolderURL
             includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey]
                                options:NSDirectoryEnumerationSkipsHiddenFiles
                                  error:&error];

请注意,这将返回绝对URL,而您的问题中的方法返回 relative 到原始目录。轻松工作,但重要的是要知道,特别是如果你删除的东西。

Note that this returns absolute URLs, whereas the method in your question returns paths that are relative to the original directory. Easily worked around, but important to know especially if you're deleting stuff.

这篇关于在Cocoa NSFIleManager的文件夹中忽略.DS_Store和Icon文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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