-1

How can i read all files list or array inside a directory. This directory/folder is inside my IOS app I check the below code but it returning me null.

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"mag1"];

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

here mag1 is my directory which contains some files. This mag1 is inside my app directory.

Larme
  • 24,190
  • 6
  • 51
  • 81
user2706827
  • 195
  • 2
  • 11
  • 1
    Log `error.desription`. If an error occurs, `contentsOfDirectoryAtPath:error:` returns nil and assigns an appropriate error object to the error parameter. – vokilam Mar 03 '14 at 14:05
  • 1
    What do you exactly mean by "my app directory"? – Levi Mar 03 '14 at 14:08
  • `Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x16df7390 {NSUnderlyingError=0x16df8640 "The operation couldn’t be completed. No such file or directory", NSFilePath=/var/mobile/Applications/68075E39-C3FD-4EE6-820C-72C8D47E9F25/mag1, NSUserStringVariant=( Folder )}` – user2706827 Mar 03 '14 at 14:09
  • The folder doesn't exist. – trojanfoe Mar 03 '14 at 14:09
  • I added a folder inside my app drag and drop, which contains some files. So do i have search the folder inside my app bundle if yes how to do that. – user2706827 Mar 03 '14 at 14:10
  • You mean the app bundle? – trojanfoe Mar 03 '14 at 14:10
  • possible duplicate of [Getting a list of files in a directory with a glob](http://stackoverflow.com/questions/499673/getting-a-list-of-files-in-a-directory-with-a-glob) – Thomas Keuleers Mar 03 '14 at 14:30
  • If you dragged the folder into XCode, the folder should have automatically been added to the application bundle, but since it doesn't seem to be there, go into your application's project settings, go to the Build Phases tab and add your folder in the Copy Bundle Resources section. – Marcus Adams Mar 03 '14 at 14:47
  • All files inside the mag1 are showing but that specific folder is not showing.. – user2706827 Mar 03 '14 at 14:52

3 Answers3

4

Get the top-level app bundle folder using [[NSBundle mainBundle] bundlePath]:

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *directory = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"mag1"];

NSArray *files=[fileMgr contentsOfDirectoryAtPath:directory error:&error];
NSLog(@"mag1 directory: %@",files);
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • @user2706827 And are you sure the bundle sub-directory exists? (check by running the app in the *iOS Simulator* and then checking the app bundle contents in `~/Library/Application Support/iPhone Simulator/`). – trojanfoe Mar 03 '14 at 14:25
  • @user2706827 That just shows the folder in Xcode and unless there is a *Copy Resources* configuration to add it to the app bundle, it won't get copied across. – trojanfoe Mar 03 '14 at 14:29
  • So do i have to configure my mag1 folder in app bundle. – user2706827 Mar 03 '14 at 14:36
  • @user2706827 Yeah, you have to tell Xcode to copy it over when building the app. – trojanfoe Mar 03 '14 at 14:37
  • can you provide me any link to configure it. – user2706827 Mar 03 '14 at 14:38
  • @user2706827 https://developer.apple.com/library/mac/recipes/xcode_help-project_editor/Articles/CreatingaCopyFilesBuildPhase.html – trojanfoe Mar 03 '14 at 14:39
  • its seems your answer is correct. but i did as follows ` NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; NSFileManager *fm = [NSFileManager defaultManager]; NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil]; NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self BEGINSWITH 'mag1_'"]; NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:fltr]; ` – user2706827 Mar 03 '14 at 15:07
0

Check the below code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingString:@"/<folder_name>"];
NSError *err = nil;
 NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(@"list %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&err]);
Sushil Kumar
  • 130
  • 7
-1

Hi you can try this one

NSMutableArray *arrayValue = [[NSMutableArray alloc]init];
NSFileManager *fManager = [NSFileManager defaultManager];
NSString *itemStr;
NSArray *array = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"mag1"] error:nil];

// Add item to an array
for (itemStr in array){
    if ([[itemStr pathExtension] isEqualToString:extension]) {
        [arrayValue addObject:item];
    }
}


NSLog(@"Array value =%@",arrayValue);
Harunmughal
  • 367
  • 1
  • 4