我有一个应用程序,我在其中记录声音文件并将其存储在应用程序文档目录中.
我想要的是,它应该只包含昨天和更旧的文件,并从iPhone应用程序中的文件夹中删除所有其他文件.有没有办法做到这一点?
谢谢 ..
请查看以下代码.
//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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2114 次 |
| 最近记录: |