Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NSDictionary* properties = [[NSFileManager defaultManager]
- attributesOfItemAtPath:NSFileModificationDate
- error:&error];
- NSArray *searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
- NSString* documentsPath = [searchPaths objectAtIndex: 0];
- NSError* error = nil;
- NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];
- if(error != nil) {
- NSLog(@"Error in reading files: %@", [error localizedDescription]);
- return;
- }
- // sort by creation date
- NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];
- for(NSString* file in filesArray) {
- NSString* filePath = [iMgr.documentsPath stringByAppendingPathComponent:file];
- NSDictionary* properties = [[NSFileManager defaultManager]
- attributesOfItemAtPath:filePath
- error:&error];
- NSDate* modDate = [properties objectForKey:NSFileModificationDate];
- if(error == nil)
- {
- [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
- file, @"path",
- modDate, @"lastModDate",
- nil]];
- }
- }
- // sort using a block
- // order inverted as we want latest date first
- NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
- ^(id path1, id path2)
- {
- // compare
- NSComparisonResult comp = [[path1 objectForKey:@"lastModDate"] compare:
- [path2 objectForKey:@"lastModDate"]];
- // invert ordering
- if (comp == NSOrderedDescending) {
- comp = NSOrderedAscending;
- }
- else if(comp == NSOrderedAscending){
- comp = NSOrderedDescending;
- }
- return comp;
- }];
- + (NSDate*) getModificationDateForFileAtPath:(NSString*)path{
- struct tm* date; // create a time structure
- struct stat attrib; // create a file attribute structure
- stat([path UTF8String], &attrib); // get the attributes of afile.txt
- date = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure
- NSDateComponents *comps = [[NSDateComponents alloc] init];
- [comps setSecond: date->tm_sec];
- [comps setMinute: date->tm_min];
- [comps setHour: date->tm_hour];
- [comps setDay: date->tm_mday];
- [comps setMonth: date->tm_mon + 1];
- [comps setYear: date->tm_year + 1900];
- NSCalendar *cal = [NSCalendar currentCalendar];
- NSDate *modificationDate = [[cal dateFromComponents:comps] addTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT]];
- [comps release];
- return modificationDate;
- NSInteger lastModifiedSort(id path1, id path2, void* context)
- {
- return [[path1 objectForKey:@"lastModDate"] compare:
- [path2 objectForKey:@"lastModDate"]];
- }
- -(void)filesByModDate
- {
- NSString* path = @"/";
- NSError* error = nil;
- NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
- error:&error];
- if(error == nil)
- {
- NSMutableArray* filesAndProperties = [NSMutableDictionary
- dictionaryWithCapacity:[files count]];
- for(NSString* path in files)
- {
- NSDictionary* properties = [[NSFileManager defaultManager]
- attributesOfItemAtPath:NSFileModificationDate
- error:&error];
- NSDate* modDate = [properties objectForKey:NSFileModificationDate];
- if(error == nil)
- {
- [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
- path, @"path",
- modDate, @"lastModDate",
- nil]];
- }
- }
- NSArray* sortedFiles = [files sortedArrayUsingFunction:&lastModifiedSort
- context:nil];
- // SUCCESS!!
- NSLog(@"sortedFiles: %@", sortedFiles);
- }
- else
- {
- NSLog(@"Encountered error while accessing contents of %@: %@", path, error);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement