Here is my code.
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];
}
}
I am getting the error logo :
Error Domain=NSCocoaErrorDominCode=516 “The operation couldn’t be
completed .(Cocoa arrow 516.)” userInfo=0x681abf0NSUnderlyingError =0x681b920 “The operation couldn’t be completed
.File exists”
abc folder there is no song name “sg.mp3” but I’m still getting the file exists error. I don’t know where I did mistake?
There is two issues in your code:
NSString *toPath = [[pathSong objectAtIndex:0] stringByAppendingPathComponent:@"Songs"];and
Then if copy occurs, it will copy the Sg.mp3 file as Songs without any type.
So you need to write it like:
This code will remove the file if exist at destination and then copy the
sg.mp3fromabcfolder toSongsfolder with the nameyourFileName.mp3