How to delete files from iPhone document catalog older than two days

I have an application in which I record audio files and save them in a Document Directory application.

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

Thanks..

-1
source share
3 answers

Please review the following code.

//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); } } } 
+7
source

If you were looking a little, you would get the following:

  [fileManager removeItemAtPath: fullPath error:NULL]; 

So, you can get the creation date of your file using something like this:

 NSFileManager* fileManager = [NSFileManager defaultManager]; NSDictionary* dict = [fileManager attributesOfItemAtPath:filePath error:nil]; NSDate *date = (NSDate*)[dict objectForKey: NSFileCreationDate]; 

Compare these dates in the if condition and delete these files.

UPDATE 1: Working code to delete files older than two days.

 // Code to delete images older than two days. #define kDOCSFOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] NSFileManager* fileManager = [[[NSFileManager alloc] init] autorelease]; NSDirectoryEnumerator* en = [fileManager enumeratorAtPath:kDOCSFOLDER]; NSString* file; while (file = [en nextObject]) { NSLog(@"File To Delete : %@",file); NSError *error= nil; NSString *filepath=[NSString stringWithFormat:[kDOCSFOLDER stringByAppendingString:@"/%@"],file]; NSDate *creationDate =[[fileManager attributesOfItemAtPath:filepath error:nil] fileCreationDate]; NSDate *d =[[NSDate date] dateByAddingTimeInterval:-2*24*60*60]; NSDateFormatter *df=[[NSDateFormatter alloc]init];// = [NSDateFormatter initWithDateFormat:@"yyyy-MM-dd"]; [df setDateFormat:@"EEEE d"]; NSString *createdDate = [df stringFromDate:creationDate]; NSString *twoDaysOld = [df stringFromDate:d]; NSLog(@"create Date----->%@, two days before date ----> %@", createdDate, twoDaysOld); // if ([[dictAtt valueForKey:NSFileCreationDate] compare:d] == NSOrderedAscending) if ([creationDate compare:d] == NSOrderedAscending) { if([file isEqualToString:@"RDRProject.sqlite"]) { NSLog(@"Imp Do not delete"); } else { [[NSFileManager defaultManager] removeItemAtPath:[kDOCSFOLDER stringByAppendingPathComponent:file] error:&error]; } } } 
+2
source

Please check the following code:

 NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil]; for (NSString *file in dirContents) { NSError *error= nil; NSDictionary *dictAtt = [[NSFileManager defaultManager] attributesOfItemAtPath:/*file path*/ error:&error]; NSDate *d =[[NSDate date] dateByAddingTimeInterval:-86400]; if ([[dictAtt valueForKey:NSFileCreationDate] compare:d] == NSOrderedAscending) { [[NSFileManager defaultManager] removeItemAtPath:/*file path*/ error:&error]; } } 
0
source

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


All Articles