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]; } } }
source share