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

iphone:从文档目录文件夹复制或移动文件,iphone,ios,Iphone,Ios,这是我的密码 NSArray *pathSong = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"]; NSString *fromPath=[[pathSong objectAtIndex: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;
    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];
        }
    }
我得到了错误标志:

错误域=NSCOCAERRORDOMINCODE=516“无法执行该操作 已完成。(箭头516。)“用户信息=0x681abf0

NSUnderlyingError=0x681b920“该操作无法完成 .文件存在“


abc文件夹没有歌曲名“sg.mp3”,但我仍然得到文件存在错误。我不知道我哪里出错了?

我想这是因为你试图用你的副本覆盖一个文件

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


您的意思是
如果(!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];
    }

代码中有两个问题:

  • 如果文件已经存在,则需要删除该文件
  • 您需要为目标文件指定一个名称,这意味着如果您使用类似的名称:
  • NSString*toPath=[[pathSong objectAtIndex:0]stringByAppendingPathComponent:@“歌曲”]

    然后,如果复制发生,它会将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];
       }
    }
    

    此代码将删除目标中存在的文件,然后将
    sg.mp3
    abc
    文件夹复制到名为
    yourFileName.mp3

    Songs
    文件夹,根据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;
     }
    

    你能在你的有问题的代码上设置代码标签吗?你正试图复制一个文件而不是另一个文件?@FaddishWorm:我想将一个文件复制到另一个文件夹。@Jonathan Plackett:如果你在复制之前删除源文件,它将显示一个错误:没有这样的文件,错误代码260必须在开始复制之前手动创建目录。。下面给出的答案中提到了代码。
    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;
     }