NSFileManager로 파일과 디텍토리를 복사/이동/삭제하는 간단한 예제 소스입니다. 각 단계는 enter를 로그창에 입력하면 진행되면 아래와 같은 순서로 작업을 합니다.

  1. test 디렉토리 생성
  2. test 디렉토리를 test2로 변경
  3. test.txt 파일을 test2 디렉토리 아래에 new_test.txt로 복사
  4. test2 디렉토리로 이동
  5. new_test.txt를 re_test.txt로 변경
  6. re_test.txt 삭제
  7. 상위 디렉토리로 이동
  8. test2 디렉토리 삭제
테스트 전에 실행파일이 있는 디렉토리에 test.txt란 파일을 vi나 편집기를 이용해 만들어 놓으셔야 합니다.

아래 이미지의 좌측은 Log창에서의 진행화면이며 우측은 터미널에서 진행화면입니다. 터미널에서 확인 후에 로그 창에서 [enter]를 입력하면서 한단계씩 진행합니다.

사용자 삽입 이미지

아래는 소스파일입니다. 별다른 내용이 없으므로 간단한 주석으로 설명을 대치하였습니다.

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

    NSFileManager *FileManager;
    FileManager = [NSFileManager defaultManager];

    /** 현재 디렉토리에서 test란 디렉토리를 생성 */
    if ([FileManager createDirectoryAtPath:@"test" attributes:nil] == NO ) {
        NSLog(@"Fail to create directory");
        return 0;
    }
    NSLog(@"create directory [press return]");
    getchar();
   
    /** 현재 디렉토리에 test.txt 파일이 있는지 검사 */
    if ([FileManager fileExistsAtPath:@"test.txt"] == NO) {
        NSLog(@"test.txt file not exist");
        return 0;
    }
   
    /** 생성된 test 디렉토리를 test2로 변경 */
    [FileManager movePath: @"test" toPath: @"test2" handler:nil];
    NSLog(@"move directory [press return]");
    getchar();
   
    /** test.txt 파일을 test2 밑에 new_test.txt로 복사 */
    [FileManager copyPath: @"test.txt" toPath: @"./test2/new_test.txt"
        handler:nil];
    NSLog(@"copy file [press return]");
    getchar();
   
    /** 현재 디렉토리를 test2로 이동 후에 new_test.txt를 re_test.txt로 변경 */
    [FileManager changeCurrentDirectoryPath: @"test2"];
    [FileManager movePath: @"new_test.txt" toPath: @"re_test.txt"
        handler:nil];
    NSLog(@"move file [press return]");
    getchar();
   
    /** re_test.txt 파일 삭제 */
    [FileManager removeFileAtPath: @"re_test.txt" handler:nil];
    NSLog(@"delete file [press return]");
    getchar();
   
    /** 현재 디렉토리를 이전 디렉토리로 이동후에 test2 디렉토리 삭제 */
    [FileManager changeCurrentDirectoryPath: @".."];
    [FileManager removeFileAtPath: @"test2" handler:nil];
    NSLog(@"delete directory");
   
    [pool release];

    return 0;
}
AND

* NSFileManager : - (NSString *)currentDirectoryPath
    어플리케이션의 현재 디렉토리를 반환합니다.

* NSBundle  : - (NSString *)bundlePath
    어플리케이션의 번들 디렉토리를 반환합니다.

* NSString *NSHomeDirectory(void)
    현재 사용자의 홈 디렉토리를 반환합니다.

* NSString *NSHomeDirectoryForUser(NSString *userName)
    특정 계정(userName) 사용자의 홈디렉토리를 반환합니다.

* NSTemporaryDirectory()
    임시(temp)로 사용할 수 있는 디렉토리를 반환합니다.

* NSString *NSUserName(void)
    현재 사용자의 계정 이름을 반환합니다.

* NSString *NSFullUserName(void)
    현재 사용자의 전체 이름을 반환합니다.

#import <Foundation/Foundation.h>

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

    // insert code here...
    NSLog(@"Cur Path: %@",
        [[NSFileManager defaultManager] currentDirectoryPath]);
    NSLog(@"Bundle Path: %@", [[NSBundle mainBundle] bundlePath]);
   
    NSLog(@"Home Path: %@", NSHomeDirectory());
    NSLog(@"User Home Path: %@",
        NSHomeDirectoryForUser(NSUserName()));
    NSLog(@"Temp Path: %@", NSTemporaryDirectory());
   
    NSLog(@"User Name: %@", NSFullUserName()); 
   
    [pool release];
    return 0;
}

실행 결과는 아래와 같습니다.
사용자 삽입 이미지
AND