2012-10-13 2 views
1

Google 검색을 시도했지만 xcode를 통해 Mac에서 파일 및 폴더를 검색하는 데 필요한 정보가 거의 없습니다.파일 및 폴더 용 드라이브 검색

어떻게 할 수 있습니까? 모든 코드 샘플 등.

델파이에서 프로그램하는 데 익숙했지만 경로 검색을위한 스 니펫은 여기에 있습니다.

procedure SearchFolders(path:string); 
var 
    sr : tsearchrec; 
    res: integer; 
    i:integer; 
begin 
    path:= includetrailingpathdelimiter(path); 
    res:= findfirst(path+'*.*',faAnyfile,sr); 
    while res = 0 do begin 
    application.processmessages; 
    if (sr.name <> '.') and (sr.name <> '..') then 
     if DirectoryExists(path + sr.name) then 
     SearchFolders(path + sr.name) 
     else 
      FileProcess.Add(path + sr.name); 
      FileSize:=FileSize+sr.Size; 
    res := findnext(sr); 
    end; 
    findclose(sr); 
end; 

활성화하려면 해당 SearchFolders ('C : \'); 경로를 검색하여 문자열 목록에 저장합니다.

xcode 내에서 osx를 어떻게 처리합니까?

답변

1

불행히도, 귀하의 코드를 완전히 이해하지 못했습니다. 그러나 일반적으로 NSFileManager을 사용하여 파일 시스템을 조사합니다.

- (NSArray *) listFilesAtPath:(NSString*)path { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    BOOL isDir; 
    if(([fileManager fileExistsAtPath:path isDirectory:&isDir] == NO) && isDir) { 
     // There isn't a folder specified at the path. 
     return nil; 
    } 

    NSError *error = nil; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    NSArray *folderItems = [fileManager contentsOfDirectoryAtURL:url 
          includingPropertiesForKeys:[NSArray arrayWithObjects:NSURLNameKey, NSURLIsDirectoryKey, nil] 
               options:NSDirectoryEnumerationSkipsHiddenFiles 
                error:&error]; 

    if (error) { 
     // Handle error here 
    } 
    return folderItems; 
} 

여기에이 방법을 사용하는 것입니다 방법의 예는 다음과 같습니다 :

는 예를 들어, 특정 경로에있는 모든 파일 (예 : 파일 및 폴더)를 나열하려면 다음을 수행 할 수

NSArray *folderItems = [self listFilesAtPath:@"/Users/1Rabbit/Desktop"]; 
for (NSURL *item in folderItems) { 
    NSNumber *isHidden = nil; 

    [item getResourceValue:&isHidden forKey:NSURLIsDirectoryKey error:nil]; 
    if ([isHidden boolValue]) { 
     NSLog(@"%@ dir", item.path); 
    } 
    else { 
     NSLog(@"%@", item.path); 
    } 
}