1

A playlist file .m3u contains entries available on an external device (a USB key in this case) such as:

/Volumes/KINGSTON/folder/mytitle.mp3

I'd like to check if the file exists:

NSURL *url = [NSURL URLWithString:@"/Volumes/KINGSTON/folder/mytitle.mp3"];
NSFileManager *manager = [NSFileManager defaultManager];
NSLog(@"%d",[manager fileExistsAtPath:[url absoluteString]]); //returns 0. I expect 1

I also tried:

NSURL *u = [[NSURL alloc]initWithScheme:@"/Volumes" host:@"/KINGSTON" path:@"/folder/mytitle.mp3"];
NSLog(@"%d",[manager fileExistsAtPath:[u absoluteString]]); //0

What did I do wrong?

Thanks,

Roland

roland
  • 7,695
  • 6
  • 46
  • 61

2 Answers2

4
NSURL *url = [NSURL URLWithString:@"/Volumes/KINGSTON/folder/mytitle.mp3"];

That string does not describe a URL. It's a pathname. Use fileURLWithPath:.

NSLog(@"%d",[manager fileExistsAtPath:[url absoluteString]]);

absoluteString does not return a path; it returns a string describing a URL. Use path.

Or, better yet, use checkResourceIsReachableAndReturnError:.

I also tried:

NSURL *u = [[NSURL alloc]initWithScheme:@"/Volumes" host:@"/KINGSTON" path:@"/folder/mytitle.mp3"];

/Volumes isn't a scheme, /KINGSTON isn't a host, and /folder/mytitle.mp3 is a path but does not refer to anything that exists.

The scheme for a file URL is file:, and the host is generally either localhost or the empty string. The path of a file URL is the complete absolute path to the file.

Community
  • 1
  • 1
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
1

In your first example, you need to use +[NSURL fileURLWithPath:]. In your second example, I see what you're trying to do, but you're simply going about it the wrong way.

I assume there's a reason you're bothering with NSURL when you have the path you could pass directly to -fileExistsAtPath:?

Extra Savoir-Faire
  • 6,026
  • 4
  • 29
  • 47