2009-09-28 74 views
0

我只是在學習Objective-C/Cocoa,我有點不確定如何擴展我的代碼來顯示找到的文件的大小。擴展到列表FileSize?

EDIT 2

我採取從下面SMorgan &彼得的意見,並將其加工成代碼...評論居多。

// ------------------------------------------------------------------- ** 
// DISC: FILEWALKER ..... (cocoa_fileWalker.m) 
// DATE: 28th September 2009 
// DESC: List all "*.exr" files in specified directory 
// ------------------------------------------------------------------- ** 

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSString *fileName; 
    NSDictionary *attrDir; 
    NSError *myError; 
    NSNumber *fileSize; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    NSString *rootPath = [@"~/Pictures" stringByExpandingTildeInPath]; 
    NSMutableArray *fileArray = [[NSMutableArray alloc] init]; 
    NSMutableString *newPath = [[NSMutableString alloc] init]; 

    NSLog(@"Home: %@",rootPath); 

    for(fileName in [manager enumeratorAtPath:rootPath]){ 
     if ([[fileName pathExtension] isEqual:@"exr"]) { 
      [fileArray addObject:fileName]; 

      [newPath setString:rootPath]; 
      [newPath appendString:@"/"]; 
      [newPath appendString:fileName]; 
      attrDir = [manager attributesOfItemAtPath:newPath error:&myError]; 
      fileSize = [attrDir objectForKey: @"NSFileSize"]; 
      NSLog(@"File: /%@ Size: %@", fileName, fileSize); 
     } 
    } 

    [fileArray release]; 
    [newPath release]; 
    [pool drain]; 
    return 0; 
} 
// ------------------------------------------------------------------- ** 

歡呼加里

回答

2

你會使用-[NSFileManager fileAttributesAtPath:traverseLink:](或者,如果你只指定10.5+ -[NSFileManager attributesOfItemAtPath:error:]),然後從返回的字典閱讀NSFileSize。

+0

我可以把它工作到我的(文件名...循環,只是不知道作爲一個初學者如何我可以在我現有的代碼中工作嗎? – fuzzygoat 2009-09-28 14:29:42

+1

是的,事實上你必須這樣做。 'enumeratorAtPath:'產生的路徑名與你列舉的目錄相關,它們不是絕對路徑名,所以你不能將它們收集到一個數組中,稍後在上下文之外使用它們。使用'stringByAppendingPathComponent:'來將葉子路徑融合到目錄路徑。 – 2009-09-28 15:02:14