Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the file extension from a list of files in an NSArray in iOS 5

Tags:

ios

Working in iOS 5 I have read a list of file names from my documents directory the array is called "file list". I am trying to get a list of file names without extensions. Only the last name in my list has the extension removed. Any ideas?

- (IBAction)getFile:(id)sender
{  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =[paths objectAtIndex:0];
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
    NSString *names = [[[fileList valueForKey:@"description"]componentsJoinedByString:@"\n"]stringByDeletingPathExtension];

    NSLog(@"File Name Is \n%@",names);    
    showFile.text = names; 
}
like image 509
Ron Bledsoe Avatar asked Jul 21 '12 21:07

Ron Bledsoe


1 Answers

- (IBAction)getFile:(id)sender
{  
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =[paths objectAtIndex:0];
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
    NSString *names = nil;
    for (NSString *name in fileList){
        if (!names) names = [[name lastPathComponent] stringByDeletingPathExtension];
        else names = [names stringByAppendingFormat:@" %@",[[name lastPathComponent] stringByDeletingPathExtension]];
    }
    NSLog(@"File Name Is \n%@",names
}
like image 112
Jesse Gumpo Avatar answered Nov 15 '22 03:11

Jesse Gumpo