Hello i am now retrieving file from document directory in iOS.
And also i sorted file by CreationDate.
In my document directory , it have also a lot of Folders and files.
So when i retrieve all files from document directory , that folders name are also including.
I only want to retrieve (.txt) format file.
How can i do that?
Assuming all of the files have an extension, you can create a predicate and use it to filter the array. The predicate would have a format like:
@"self ENDSWITH '.txt'"
Based on your comment below, you actually have the full file NSURL
, not string file names. So you should use the predicate format:
@"absoluteString ENDSWITH '.txt'"
This is in addition to @Wain's answer and assuming that you are using a webview to load the text file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.txt'"];
filePathsArray = [filePathsArray filteredArrayUsingPredicate:predicate];
NSLog(@"files array %@", filePathsArray);
NSString *localDocumentsDirectoryVideoFilePath = [documentsDirectory
stringByAppendingPathComponent:[filePathsArray objectAtIndex:0]];
NSURL *fileUrl=[NSURL fileURLWithPath:
localDocumentsDirectoryVideoFilePath];
[_webview loadRequest:[NSURLRequest requestWithURL:fileUrl]];
Here _webview is an IBOutlet. Also, it is loading the first text file.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With