iphone:从文件目录文件夹复制或移动文件

问题描述:

这是我的代码。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

。在你的代码的两个问题:

  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

我认为它是因为你试图用你的副本覆盖文件。

检查您的权限掩码,请尝试使用缓存而不是文档目录。

你的意思if(!fileExistsAtPath)

你需要删除已经存在的文件:

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]; 
    } 

在吸取复制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; 
}