You could do something like this:
NSEnumerator *files = [[[NSFileManager defaultManager]
contentsOfDirectoryAtPath:theFolder error:&error] objectEnumerator];
NSMutableArray *arrayOfTxtFiles = [[NSMutableArray alloc] init];
while(file = [files nextObject] ) {
if( [[file pathExtension] isEqualToString:@"txt"] ) {
[arrayOfTxtFiles addObject: file];
}
}
NSLog( @"count of .txt files is %d", [arrayOfTxtFiles count] );
arrayOfTxtFiles
contains all the .txt filenames.
I'm basing my code off your first code snippet, although there may be an even more elegant way to do what you're trying to do.