0

I'm looking to return an NSArray which contains all of the files URLS located within a particular folder in iOS.

In this case I'm looking inside the Ryan/Bob/FolderWithInfo - which contains 4 files. I want to get the file URL of each of these files.

Here is a example of an output I'm looking for :

NSArray *array = @[

@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout1.csv",
@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout2.csv",
@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout3.csv",
@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout4.csv",
];

How is this achievable?

Edit: I have tried this and thought of maybe prepending the main path after, but there has to be a more efficient way :)

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
directory = [NSString stringWithFormat:@"%@/%@/%@/%@", directory, @"Ryan, @"Bob", @"FolderWithInfo"];

NSArray *files=[fileMgr contentsOfDirectoryAtPath:directory error:&error];
NSLog(@"mag1 directory: %@",files);

Thanks.

ZeMoon
  • 20,054
  • 5
  • 57
  • 98
Fudgey
  • 3,793
  • 7
  • 32
  • 53
  • possible duplicate of [How do I get an NSArray of filenames of all files in a given directory in my app?](http://stackoverflow.com/questions/1272042/how-do-i-get-an-nsarray-of-filenames-of-all-files-in-a-given-directory-in-my-app) – Leandros Apr 30 '14 at 15:28
  • http://stackoverflow.com/a/907444/2043580 – ZeMoon Apr 30 '14 at 15:29
  • But note that `NSFileManager` only returns relative file names. So you should use `[basePath stringByAppendingPathComponent:fileName]` in order to get the absolute path. –  Apr 30 '14 at 15:31
  • @CodeMound You get the filenames, and afterwards you can simply prepend the folderpath. – Leandros Apr 30 '14 at 15:32
  • @CodeMound the `NSFileManager` methods that return URLs give full URLs. It's the ones that return arrays of `NSString` that return filenames. – rmaddy Apr 30 '14 at 15:35
  • @Ben Did you take any time to look at the docs for `NSFileManager`? Have you tried anything yet? – rmaddy Apr 30 '14 at 15:36
  • @Leandros Thats what i was thinking but there has to be a more efficient way :) – Fudgey Apr 30 '14 at 15:37
  • @Ben Yeah, that's right. It's not the most sophisticated way, though. – Leandros Apr 30 '14 at 15:38
  • @maddy correct. contentsOfDirectoryAtURL - the method that should be used - returns filenames –  Apr 30 '14 at 15:39
  • @maddy I've just revised my answer to show what i have tried :) – Fudgey Apr 30 '14 at 15:41
  • @CodeMound That method returns file URLs, not filenames. – rmaddy Apr 30 '14 at 15:53
  • Note that the URLs returned by `contentsOfDirectoryAtURL` can have a different base path than the directory you passed in if the directory has symbolic links. To avoid surprises you therefore might want to grab the `lastPathComponent` and append that to the directory URL and use those URLs instead. – phatmann May 25 '15 at 11:49

1 Answers1

1

As said in the comments, use - (NSArray *)contentsOfDirectoryAtURL:(NSURL *)url includingPropertiesForKeys:(NSArray *)keys options:(NSDirectoryEnumerationOptions)mask error:(NSError **)error.

NSFileManager *fm = [NSFileManager defaultManager];

NSError *error;
NSURL *url = [NSURL URLWithString:@"file:///Users/leandros/temp"];
NSArray *urls = [fm contentsOfDirectoryAtURL:url includingPropertiesForKeys:nil options:0 error:&error];
Leandros
  • 16,805
  • 9
  • 69
  • 108