[英]How to copy all text file from document directory in iOS?
I need to Copy all text files (.txt) from document directory into my folder name "Text". 我需要将文档目录中的所有文本文件(.txt)复制到我的文件夹名称“文本”中。
I can copy only one files. 我只能复制一个文件。
I want to copy like (*.txt)
with one touch. 我想一键复制
(*.txt)
。
Here codes is my single file copy. 这里的代码是我的单个文件副本。
self.fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *cutExtension = [tblGetNameToMove.textLabel.text stringByDeletingPathExtension];
NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",cutExtension]];
NSString *fileName = [NSString stringWithFormat:@"%@/Text/%@.txt",documentsDirectory,cutExtension];
[self.fileManager copyItemAtPath:txtPath toPath:fileName error:&error];
How can i do that? 我怎样才能做到这一点?
Get all the contents in the folder 获取文件夹中的所有内容
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileList = [[NSFileManager defaultManager] directoryContentsAtPath:documentsDirectory];
Enumerate over the files list and check if it constains .txt 枚举文件列表并检查其是否包含.txt
[fileList enumerateObjectsUsingBlock:^(NSString *string, NSUInteger index, BOOL *stop) {
if ([string hasSuffix:@".txt"]) {
[[NSFileManager defaultManager]copyItemAtPath:strdestination toPath:toPath error:&Error];
}
}];
Update 更新资料
Updated my answer according to comments by Martin R 根据Martin R的评论更新了我的答案
Update 2 更新2
As pointed out by Hot Licks use 正如Hot Licks指出的那样
if ([[string pathExtension] isEqualToString:@"txt"]) {
To check for the file extension. 检查文件扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.