Krunal
Krunal

Reputation: 6490

Get Files Inside a folder in Document directory in iPhone

I am new to iPhone,

I made an app in which, i am downloading a book and storing it in a folder called book1Folder which is inside Document directory.

now i wants name of all books in my array, there are 2 books inside a book1Folder but when i write this code it shows me array count is 3.

Here is my code snippet,

-(void)viewWillAppear:(BOOL)animated{
    NSString *files;
    NSString *Dir=[self applicationDocumentsDirectory];
    Dir=[Dir stringByAppendingPathComponent:@"book1Folder"];

    NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:Dir];

    Downloadedepubs = [[NSMutableArray alloc]init];
    while(files = [direnum nextObject])
    {
        if([[files pathExtension] isEqualToString:@"epub"])
            NSLog(@"files=%@",files);
            [Downloadedepubs addObject:files];
    }
}

My log shows only name of 2 book, but when i loop through array it contains 3 objects.

[Downloadedepubs ObjectAtIndex:0]=.DS_Store;
[Downloadedepubs ObjectAtIndex:1]=abcd;
[Downloadedepubs ObjectAtIndex:2]=pqrs;

What is that .DS_Store why it is coming ?

Any help will be appreciated.

Upvotes: 1

Views: 1623

Answers (3)

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

Try this:

  NSDirectoryEnumerator*    e = [[NSFileManager defaultManager] enumeratorAtPath:yourPathhere];

// Ignore any files except XYZ.epub
for (NSString*  file in e)
{
    if (NSOrderedSame == [[file pathExtension] caseInsensitiveCompare:@"epub"])
    {
           // Do something with file.epub
               [Downloadedepubs addObject:files];

    }
    else if ([[[e fileAttributes] fileType] isEqualToString:NSFileTypeDirectory])
    {
        // Ignore any subdirectories
        [e skipDescendents];
    }
}

Upvotes: 0

Samet DEDE
Samet DEDE

Reputation: 1621

Get your if block in curly brackets. It affects only first line after if.

Like below;

if([[files pathExtension] isEqualToString:@"epub"]) {
            NSLog(@"files=%@",files);
            [Downloadedepubs addObject:files];
}

Upvotes: 3

cloosen
cloosen

Reputation: 993

.DS_Store is a hiden file in mac to store your files message in the Folder.you can delete it

Upvotes: 0

Related Questions