Search code examples
iphonexcodensdocumentdirectory

unable to omit @".DS_Store" file while fetching contents of Document Drectory


I am working on storing a list of audio files into my document directory and then fetching them.

It gives me a list of audio files along with this it gives me a file with name @".DS_Store". While fetching content I want to leave this file of documents directory.

Is there any way I can get rid of this while fetching the audio list other than removing this from array or apply a @".DS_Store" check.

What exactly is the reason for this.?

#pragma mark - Saving Audio in Document Directory
-(void)saveAudioinDocumentDirectory:(ASIHTTPRequest *)theRequest
{
    /*save the Audio file in Document Directory */
    NSFileManager *fileManager=[NSFileManager defaultManager];

    NSLog(@"GOT THE SIZe OF AUDIO %d",[[theRequest responseData] length]);
    NSLog(@"AUDIO ID IS %@",[[theRequest userInfo] valueForKey:@"audioIndex"]);

    /*Get the Path  to Application Documents Directory*/
    NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    /*append the neccessary file extension */

    NSString *filepathStr=[NSString stringWithFormat:@"/%@/%@.mp3",docDir,[NSString stringWithFormat:@"%@",[[theRequest userInfo] valueForKey:@"audioIndex"]]];

    /*Check if my crrent file exists in the Documents Directory*/

    if(![fileManager fileExistsAtPath:filepathStr])
    {
        /* file doesnt exists */

        /*create a NSdata of File*/
        NSData *data=[NSData dataWithData:[theRequest responseData]];
        NSLog(@"%@",filepathStr);

        if ([data length] >0 ){


            /*write the File at the Location in Documents Directory */

            [data writeToFile:filepathStr atomically:YES];
            NSLog(@"Successfully saved the file to %@", filepathStr);
        }
        else if([data length] == 0)
        {
            NSLog(@"Nothing was downloaded.");
        }

    }
    /*After saving fetch the path til documents Directory*/
    NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    /*Get the Path for Files */
    NSString *s=[folders objectAtIndex:0];

    /*Fetch the list of Files stored in Documents Directory*/

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:s error:NULL];
    NSLog(@"TOTAL NUMBER OF AUDIO FILES %d %@",[contents count],[contents objectAtIndex:0]);

    if([Audiolistforstr isEqualToString:@"AudioListForIntro"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  from document Intro IS %@",Audiolistforstr);

        /*Intro*/
        [AudioListArrForIntro removeAllObjects];
        [AudioListArrForIntro addObjectsFromArray:contents];
        if([AudioListArrForIntro containsObject:@".DS_Store"])
        {
            [AudioListArrForIntro removeObject:@".DS_Store"];

        }
        NSLog(@"FINAL LIST %@",AudioListArrForIntro);

    }
    else if([Audiolistforstr isEqualToString:@"AudioListForCredits"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  from document credit IS %@",Audiolistforstr);

        /*credits*/
        [AudioListArrForCredits removeAllObjects];
        [AudioListArrForCredits addObjectsFromArray:contents];
        if([AudioListArrForCredits containsObject:@".DS_Store"])
        {
            [AudioListArrForCredits removeObject:@".DS_Store"];

        }
    NSLog(@"FINAL LIST %@",AudioListArrForCredits);

    }


    /* Did we find anything? */
    if([Audiolistforstr isEqualToString:@"AudioListForIntro"])
    {

        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  fromRELOADNG TABLE Intro IS %@",Audiolistforstr);

        /*Intro*/
        if ([AudioListArrForIntro count] == 0)
        {

        }
        else
        {
            UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioIntroTag];
            [(UITableView *)[vw viewWithTag:tblIntroAudioListTag] reloadData];
        }
    }
    else if([Audiolistforstr isEqualToString:@"AudioListForCredits"])
    {
        //  NSLog(@"Audiolistforstr@"IntroThirdRow"  in reading audio  fromRELOADNG TABLE Intro IS %@",Audiolistforstr);

        /*Credits*/
        if ([AudioListArrForCredits count] == 0)
        {
        }

        else
        {

            /*AudioListForCredits*/

            UIView *vw=(UIView *)[self.view viewWithTag:ViewAddAudioCreditsTag];
            [(UITableView *)[vw viewWithTag:tblCreditsAudioListTag] reloadData];
        }

    }

}

Any help would be appreciated.

Thanks Vikas


Solution

  • Filter your document directory contents. For example, if you are having audio files with extension of .mp3, then you can get all the mp3 files as below:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
    
    directoryContent = [directoryContent filteredArrayUsingPredicate:
                                                [NSPredicate predicateWithFormat:@"pathExtension ==[c] %@", @"mp3"]];
    

    This will omit all other files than the mp3 files.. All the best!!!