Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFileManager copyItemAtPath complains about a nonexistent file that does exist

I am trying to copy a file using [[NSFileManager defaultManager] copyItemAtPath: toPath: error:] but it is failing with the following error:

4: The file does not exist.

The relevant code is below, and the file does exist and the path string is correct because it is created beforehand with the exact same file path string.

NSFileManager* manager = [NSFileManager defaultManager];
NSError* error;
NSString* fileName = [Sound getFileName:Title];
NSString* oldDirectory = [NSString stringWithFormat:@"%@%@/", [settings stringForKey:@"downloadFolder"], authorFolder];
NSString* oldFile = [oldDirectory stringByAppendingFormat:@"%@.mp3", fileName];
NSString* newFile = [NSString stringWithFormat:@"%@/iTunes/iTunes Media/Automatically Add to iTunes/%@.mp3", [NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES) objectAtIndex:0], fileName];
BOOL result = [manager copyItemAtPath:oldFile toPath:newFile error:&error];
if (!result && error)
{
     NSLog(oldFile);
     NSLog(@"There was an error copying the file to the iTunes directory! %@", [error localizedDescription]);
}

It's not the exact code, but all relevant code should be above. If I use [manager fileExistsAtPath:oldFile] the result is YES.

What could cause the copy to fail and say the file doesn't exist, even if it does?

UPDATE:

Issue fixed. Turns out the output folder was really Automatically Add to iTunes.localized, but I didn't notice this initially when just paging through the finder. Fixing the output path solved the issue! Thanks for the help.

like image 907
Brennan Kastner Avatar asked Mar 20 '13 06:03

Brennan Kastner


1 Answers

If any of the directories in the path of the destination don't exist, you'll get a similar error to what you'd get if the source doesn't exist. Check what [manager fileExistsAtPath:[newFile stringByDeletingLastPathComponent] isDirectory:&isDir] returns.

like image 156
Ken Thomases Avatar answered Sep 19 '22 15:09

Ken Thomases