2009-09-17 55 views
1

我構建NSmetaDataQuery找到隱形的文件夾(如「.myInvisibleFolder」)查找與聚光燈看不見的文件夾。通過構建一個NSPredicate與NSMetadataQuery

不幸的是,鎂光燈似乎並沒有被定位文件夾開頭「」,即使具體包括在謂語。

什麼工作和不工作

搜索任何非invisable文件名的作品。

搜索內容作品(kMDItemTextContent)。

沒有以「。」開頭的文件。被發現。總是返回0結果。

作爲測試,在Finder中搜索隱形內容的作品。

我在做什麼錯?有沒有另外一種方法來查找無形文件夾?

代碼:

- (void)searchForMyInvisableFolders{ 
    self.query = [[[NSMetadataQuery alloc] init] autorelease]; 

    // To watch results send by the query, add an observer to the NSNotificationCenter 
    NSNotificationCenter *nf = [NSNotificationCenter defaultCenter]; 
    [nf addObserver:self selector:@selector(queryNote:) name:nil object:self.query]; 

    // Sort results by file name 
    [self.query setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemFSName ascending:YES] autorelease]]]; 

    [self.query setDelegate:self]; 

    //Create a predicate to search for file name 
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@" (kMDItemFSName == '.myInvisibleFolder')"]; 

    //Create a predicate to search for invisible files 
    NSPredicate* invisablePredicate = [NSPredicate predicateWithFormat:@"kMDItemFSInvisible == YES"]; 

    //Compound predicate 
    NSPredicate* compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate, invisiblePredicate, nil]]; 

    // Set it to the query. 
    [self.query setPredicate:compoundPredicate];   

    // Start it. 
    [self.query startQuery]; 

} 
+0

默認情況下Spotlight排除不可見文件夾。這實際上是積極用於防止文檔包在完全寫入磁盤之前建立索引:[Spotlight和隱形文件/文件夾](https://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/MetadataIntro /Concepts/DocumentBundles.html) – Jay 2012-09-30 07:44:19

回答

2

您的代碼完全適用於我,如果我改變第一謂詞:

[NSPredicate predicateWithFormat:@" (kMDItemFSName == '.DS_Store')"]; 

是您真的叫「.myInvisableFolder」看不見的文件夾(注意您的無形拼寫錯誤)?

+0

好吧,不知道發生了什麼,但現在它正在工作。感謝您爲我測試它! – 2009-09-18 14:22:37