xcode 从文档目录中删除文件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5819117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:56:03  来源:igfitidea点击:

Remove file types from Documents Directory

iphonecocoaxcodeipad

提问by Snowy

i am trying to delete all the files in the Document Directory with the extension ".jpg", after many tries, i am still not managed to succeed.

我试图删除扩展名为“.jpg”的文档目录中的所有文件,经过多次尝试,我仍然没有成功。

the code i have already now is:

我现在已经拥有的代码是:

-(void)removeOneImage:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
}

with this code i can remove one file with a specific name.

使用此代码,我可以删除一个具有特定名称的文件。

Can anyone help me to remove all files with a specific extension?

谁能帮我删除具有特定扩展名的所有文件?

Kind regards,

亲切的问候,

Snowy

下雪

回答by Francis McGrew

    NSString *extension = @"jpg";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];  
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {

    if ([[filename pathExtension] isEqualToString:extension]) {

        [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
    }
}

回答by Chetan Bhalara

// Get the Documents directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];

// Delete the file using NSFileManager
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[documentsDirectoryPath stringByAppendingPathComponent:yourFile.txt] error:nil];

For More information

想要查询更多的信息

How to delete ALL FILES in a specified directory on the app?

如何删除应用程序指定目录中的所有文件?

回答by Rajesh Loganathan

very simple method:

非常简单的方法:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *imageName = [NSString stringWithFormat:@"demo.jpg"];
[fileManager removeItemAtPath:imageName error:NULL];

回答by Shebuka

If you prefer fast enumeration:

如果您更喜欢快速枚举:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *extension = @"jpg";

NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

for (NSString *file in contents) {
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:file];

    if ( ([[file pathExtension] isEqualToString:extension]) && [fileManager isDeletableFileAtPath:filePath] ) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
}