Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of files with specific extensions at a specified path in Objective-C

Tags:

I'm pretty new to Objective-C. I'm developing a Cocoa application. Currently I'm looking for the equivalent of this C# code in Objective C:

string[] fileList = Directory.GetFiles(DownloadPath, "*.jpg"); 

The returned strings need not necessarily be full path, since all I need is the file names. I have tried NSFileManager but so far no good yet. Thank you.

EDIT: What I've tried with NSFileManager:

[someFileManager contentsOfDirectoryAtPath:path error:nil]; 

I also want to ask: what is the format of 'path'? This sounds easy, but I'm completely clueless about MAC OS file system. The path that I'm using is from [NSOpenPanel URLs], and they looks like this:

file://localhost/Users/alex/Movies/ 

Sometimes I get the results, but some other time The returned NSArray is just empty. I'm pretty confused about this so any help would be appreciated.

EDIT2: The answer here: NSPredicate endswith multiple files, is probably a better choice. Nevertheless, thank you for your answer.

like image 337
Zekareisoujin Avatar asked Aug 03 '11 04:08

Zekareisoujin


1 Answers

This code should work:

NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; NSArray *jpgFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]]; 
like image 198
VenoMKO Avatar answered Oct 24 '22 03:10

VenoMKO