2012-08-17 218 views
1

這是我的代碼。iphone:從文件目錄文件夾複製或移動文件

NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"]; 
    NSString *fromPath=[[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"]; 
    NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"]; 
    NSError *Error; 
    if([[NSFileManager defaultManager]fileExistsAtPath:strdestination]){ 
     if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO){ 
      UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [Alert show]; 
     } 
     else{ 
      UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [Alert show]; 
     } 
    } 

我得到了錯誤的標誌:

錯誤域= NSCocoaErrorDominCode = 516 「的操作無法完成 (可可箭頭516)」 USERINFO = 0x681abf0

NSUnderlyingError = 0x681b920「操作無法完成 。文件存在」

abc文件夾沒有歌曲名稱「sg.mp3」,但我仍然收到文件存在錯誤。我不知道我犯了什麼錯誤?

+0

你能在你的代碼中設置代碼標籤嗎? – BergP 2012-08-17 11:44:41

+0

您正試圖將一個文件複製到另一個文件上? – FaddishWorm 2012-08-17 11:46:02

+0

@ FaddishWorm:我想將一個文件複製到另一個文件夾。 – user1606762 2012-08-17 11:52:40

回答

11

。在你的代碼的兩個問題:

  1. 您需要刪除該文件它已經在那裏
  2. 您需要指定目標文件的名稱,意思是如果您使用像:

NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];

[[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]; 

如果發生複製之後,它將Sg.mp3文件拷貝的歌曲沒有任何類型的。

所以你需要把它寫,如:

NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *tempPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"]; 
NSString *toPath = [tempPath stringByAppendingPathComponent:@"yourFileName.mp3"]; 
NSString *fromPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"]; 
NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"]; 
NSError *Error = nil; 

if([[NSFileManager defaultManager]fileExistsAtPath:strdestination]) 
{ 

    if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO) 
    { 
     UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [Alert show]; 
    } 
    else 
    { 
     [fileManager removeItemAtPath:strdestination error:NULL]; 
     UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [Alert show]; 
    } 
} 

此代碼是否存在於目的地將刪除該文件,然後從abc文件夾名稱yourFileName.mp3

+0

必須手動創建目錄。 代碼在下面給出的答案中提及。 – AnkitRox 2015-05-14 13:17:14

0

我認爲它是因爲你試圖用你的副本覆蓋文件。

檢查您的權限掩碼,請嘗試使用緩存而不是文檔目錄。

你的意思if(!fileExistsAtPath)

0

你需要刪除已經存在的文件:

NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"]; 
NSString *fromPath=[[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"abc"]; 
NSString *strdestination = [fromPath stringByAppendingPathComponent:@"sg.mp3"]; 
NSError *Error; 


//DELETE THE FILE AT THE LOCATION YOU'RE COPYING TO 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
[fileManager removeItemAtPath:strdestination error:NULL]; 


if([[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error]==NO){ 
     UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [Alert show]; 
    } 
else{ 
     UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Not copy" message:[NSString stringWithFormat:@"%@",Error] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [Alert show]; 
    } 
2

在吸取複製sg.mp3Songs文件夾Midhun MP答案,這裏是一個幫手

BOOL moveFile(NSString *srcPath, NSString *dstPath) 
{ 
    NSLog(@"moving %@ -> %@", srcPath, dstPath); 

    NSFileManager *fm = [NSFileManager defaultManager]; 
    if ([fm fileExistsAtPath:dstPath]) 
    { 
     // in my usecase this is a hard error, bolt to prevent overwriting 
     return NO; 
    } 

    if ([fm fileExistsAtPath:srcPath]) 
    { 
     NSError *error = nil; 
     NSString *destDir = [dstPath stringByDeletingLastPathComponent]; 
     [fm createDirectoryAtPath:destDir withIntermediateDirectories:YES attributes:nil error:nil]; 

     if ([[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:&error]==NO) 
     { 
      NSLog(@"failure declassing %@", srcPath); 
      return NO; 
     } 
     else 
     { 
      [fm removeItemAtPath:srcPath error:NULL]; // gr8t success 
      return YES; 
     } 
    } 

    return NO; 
} 
相關問題