2011-11-15 98 views
1

我有一些代碼將所有目錄從「/ Documents」複製到「Library/Caches /」。代碼如下:NSFileManager removeItemAtPath清除目錄的內容,但不清除實際的目錄本身

NSString *oldPath = [NSString stringWithFormat:@"%@/Documents/", NSHomeDirectory()]; 
NSString *newPath = [NSMutableString stringWithFormat:@"%@/Library/Caches/", NSHomeDirectory()]; 
NSError *error = nil; 

// get the list of all files and directories 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:oldPath error:&error]; 

if(error != nil) 
{ 
    NSLog(@"\n\n\n1. There was an error in the file operation: %@", [error localizedDescription]); 
    return; 
} 

for(NSString *file in fileList) 
{ 
    NSString *path = [oldPath stringByAppendingPathComponent:file]; 

    NSLog(@"The path for the file is: %@", path); 

    BOOL isDir = NO; 

    BOOL dirExists = [fileManager fileExistsAtPath:path isDirectory:(&isDir)]; 

    if(isDir && dirExists) 
    { 
     //Move to the new location 
     [fileManager copyItemAtPath:path toPath:[newPath stringByAppendingPathComponent:file] error:&error]; 
     if(error != nil) 
     { 
      NSLog(@"\n\n\n2. There was an error in the file operation: %@", [error localizedDescription]); 
     } 
     else 
     { 
      [fileManager removeItemAtPath:path error:&error]; 

      if(error != nil) 
       NSLog(@"\n\n\n3. There was an error in the file operation: %@", [error localizedDescription]); 
     } 
    } 
} 

複製工作正常。我可以看到複製到新位置的所有目錄。

問題是空目錄仍然保留在Documents文件夾中。我在文檔removeItemAtPath上看過,它說目錄中的內容被刪除。是否刪除實際目錄本身沒有提及。

有人能告訴我代碼中會出現什麼問題嗎?爲什麼空目錄仍然存在?

編輯:

在第一遍,當文件存在的目錄,而不是在圖書館/緩存,removeItemAtPath不會拋出一個錯誤。儘管如此,空目錄仍然保留在文檔中。在上面的代碼的第二遍,它試圖刪除空目錄和removeItemAtPath引發可可錯誤516,這基本上沒有找到文件 - 這很奇怪,因爲我仍然可以看到那些空目錄。

另外,我上運行iPhone模擬器4.3.2和iPhone模擬器5.0的代碼,並在監控我的Mac上的目錄結構/用戶/庫/應用程序支持/ iPhone模擬器/ ....

回答

3

由於「/ Documents /」中尾隨的「/」,可能會發生這種情況。嘗試從第2行刪除尾部「/」,或使用此:

NSString *oldPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSString *newPath = [NSString pathWithComponents:[NSArray arrayWithObjects:NSHomeDirectory() , @"Library", @"Caches", nil]]; 

NSError *error = nil; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:oldPath error:&error]; 

if (error) { 
    NSLog(@"\n\n\n1. There was an error in the file operation: %@", [error localizedDescription]); 
    return; 
} 

for (NSString *file in fileList) { 
    NSString *path = [oldPath stringByAppendingPathComponent:file]; 
    NSLog(@"The path for the file is: %@", path); 

    BOOL isDir = NO; 

    if ([fileManager fileExistsAtPath:path isDirectory:&isDir] && isDir) { 
     [fileManager copyItemAtPath:path toPath:[newPath stringByAppendingPathComponent:file] error:&error]; 
     if(error) { 
      NSLog(@"\n\n\n2. There was an error in the file operation: %@", [error localizedDescription]); 
     } else { 
      [fileManager removeItemAtPath:path error:&error]; 

      if (error) { 
       NSLog(@"\n\n\n3. There was an error in the file operation: %@", [error localizedDescription]); 
      } 
     } 
    } 
} 
+0

嗨chown,謝謝你的迴應。可悲的是,我嘗試了你發佈的代碼,但它並沒有解決它。我想知道這只是一個模擬器問題? – Sid

+0

@Sid這很可能是因爲代碼看起來不錯。如果它在設備上工作,那麼它們必須有權限問題(或類似的問題),以防止sim去除該目錄。 – chown

+0

因此,在設備上進行測試不會顯示任何文件操作錯誤的日誌;我認爲這意味着它可以在設備上運行,但我仍然無法100%確定,因爲我不知道有任何方法可以查看我自己的文件系統....感謝您的幫助! – Sid

相關問題