Advertisement
Guest User

Get directory contents in date modified order

a guest
Dec 14th, 2011
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. NSDictionary* properties = [[NSFileManager defaultManager]
  2. attributesOfItemAtPath:NSFileModificationDate
  3. error:&error];
  4.  
  5. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
  6. NSString* documentsPath = [searchPaths objectAtIndex: 0];
  7.  
  8. NSError* error = nil;
  9. NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];
  10. if(error != nil) {
  11. NSLog(@"Error in reading files: %@", [error localizedDescription]);
  12. return;
  13. }
  14.  
  15. // sort by creation date
  16. NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];
  17. for(NSString* file in filesArray) {
  18. NSString* filePath = [iMgr.documentsPath stringByAppendingPathComponent:file];
  19. NSDictionary* properties = [[NSFileManager defaultManager]
  20. attributesOfItemAtPath:filePath
  21. error:&error];
  22. NSDate* modDate = [properties objectForKey:NSFileModificationDate];
  23.  
  24. if(error == nil)
  25. {
  26. [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
  27. file, @"path",
  28. modDate, @"lastModDate",
  29. nil]];
  30. }
  31. }
  32.  
  33. // sort using a block
  34. // order inverted as we want latest date first
  35. NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
  36. ^(id path1, id path2)
  37. {
  38. // compare
  39. NSComparisonResult comp = [[path1 objectForKey:@"lastModDate"] compare:
  40. [path2 objectForKey:@"lastModDate"]];
  41. // invert ordering
  42. if (comp == NSOrderedDescending) {
  43. comp = NSOrderedAscending;
  44. }
  45. else if(comp == NSOrderedAscending){
  46. comp = NSOrderedDescending;
  47. }
  48. return comp;
  49. }];
  50.  
  51. + (NSDate*) getModificationDateForFileAtPath:(NSString*)path{
  52. struct tm* date; // create a time structure
  53. struct stat attrib; // create a file attribute structure
  54.  
  55. stat([path UTF8String], &attrib); // get the attributes of afile.txt
  56.  
  57. date = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure
  58.  
  59. NSDateComponents *comps = [[NSDateComponents alloc] init];
  60. [comps setSecond: date->tm_sec];
  61. [comps setMinute: date->tm_min];
  62. [comps setHour: date->tm_hour];
  63. [comps setDay: date->tm_mday];
  64. [comps setMonth: date->tm_mon + 1];
  65. [comps setYear: date->tm_year + 1900];
  66.  
  67. NSCalendar *cal = [NSCalendar currentCalendar];
  68. NSDate *modificationDate = [[cal dateFromComponents:comps] addTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT]];
  69.  
  70. [comps release];
  71.  
  72. return modificationDate;
  73.  
  74. NSInteger lastModifiedSort(id path1, id path2, void* context)
  75. {
  76. return [[path1 objectForKey:@"lastModDate"] compare:
  77. [path2 objectForKey:@"lastModDate"]];
  78. }
  79.  
  80. -(void)filesByModDate
  81. {
  82. NSString* path = @"/";
  83. NSError* error = nil;
  84.  
  85. NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
  86. error:&error];
  87. if(error == nil)
  88. {
  89. NSMutableArray* filesAndProperties = [NSMutableDictionary
  90. dictionaryWithCapacity:[files count]];
  91. for(NSString* path in files)
  92. {
  93. NSDictionary* properties = [[NSFileManager defaultManager]
  94. attributesOfItemAtPath:NSFileModificationDate
  95. error:&error];
  96. NSDate* modDate = [properties objectForKey:NSFileModificationDate];
  97.  
  98. if(error == nil)
  99. {
  100. [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
  101. path, @"path",
  102. modDate, @"lastModDate",
  103. nil]];
  104. }
  105. }
  106.  
  107. NSArray* sortedFiles = [files sortedArrayUsingFunction:&lastModifiedSort
  108. context:nil];
  109.  
  110. // SUCCESS!!
  111. NSLog(@"sortedFiles: %@", sortedFiles);
  112. }
  113. else
  114. {
  115. NSLog(@"Encountered error while accessing contents of %@: %@", path, error);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement