How to filter file with extension from document directory in iOS?

Hello, now I am extracting a file from the document directory in iOS.

And also I sorted the file using CreationDate.

He also has many folders and files in my document directory.

Therefore, when I extract all files from the document directory, this folder name also includes.

I only want to get the file format (.txt).

How can i do this?

+4
source share
2 answers

Assuming all files have an extension, you can create a predicate and use it to filter the array. The predicate would have a format such as:

@"self ENDSWITH '.txt'" 

Based on your comment below, you actually have the full NSURL file, not string file names. Therefore, you should use the predicate:

 @"absoluteString ENDSWITH '.txt'" 
+5
source

This is in addition to the @ Wain answer and it is assumed that you are using web browsing to download a 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. In addition, this is the loading of the first text file.
Hope this helps.

+2
source

Source: https://habr.com/ru/post/1493219/


All Articles