如何从iPhone的文档目录中删除超过两天的文件 [英] How to delete files from iPhone's document directory which are older more than two days

查看:61
本文介绍了如何从iPhone的文档目录中删除超过两天的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以记录声音文件并将其存储在应用程序文档目录中的应用程序.

I have an App in which I record the sound files and store it in apps Document Directory.

我想要的是,它应该只包含昨天和几天以前的旧文件,并从iPhone应用程序的文件夹中删除所有其他文件.有什么办法吗?

What I want is, it should contain only yesterday and to days older files and remove all others from the folder in iPhone app. Is there any way to do this?

谢谢..

推荐答案

请查看以下代码.

//Get the Document directory path.
 #define kDOCSFOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]

//Delete files by iterating items of the folder.
NSFileManager* fm = [[[NSFileManager alloc] init] autorelease];
NSDirectoryEnumerator* en = [fm enumeratorAtPath:kDOCSFOLDER];    
NSError* err = nil;
BOOL res;

NSString* file;
while (file = [en nextObject]) {
            // Date comparison.
    NSDate   *creationDate = [[fm attributesOfItemAtPath:file error:nil] fileCreationDate];
    NSDate *yesterDay = [[NSDate date] dateByAddingTimeInterval:(-1*24*60*60)];

    if ([creationDate compare:yesterDay] == NSOrderedAscending)
    {
        // creation date is before the Yesterday date
        res = [fm removeItemAtPath:[kDOCSFOLDER stringByAppendingPathComponent:file] error:&err];

        if (!res && err) {
            NSLog(@"oops: %@", err);
        }

    }

}

这篇关于如何从iPhone的文档目录中删除超过两天的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆