Get directory contents in date modified order - iphone

Is there an method to get the contents of a folder in a particular order? I'd like an array of file attribute dictionaries (or just file names) ordered by date modified.
Right now, I'm doing it this way:
get an array with the file names
get the attributes of each file
store the file's path and modified date in a dictionary with the date as a key
Next I have to output the dictionary in date order, but I wondered if there's an easier way? If not, is there a code snippet somewhere which will do this for me?
Thanks.

nall's code above pointed me in the right direction, but I think there are some mistakes in the code as posted above. For instance:
Why is filesAndProperties allocated using NMutableDictonary rather than an NSMutableArray?
NSDictionary* properties = [[NSFileManager defaultManager]
attributesOfItemAtPath:NSFileModificationDate
error:&error];
The code above is passing the wrong parameter for attributesOfItemAtPath - it should be attributesOfItemAtPath:path
Your are sorting the files array, but you should be sorting filesAndProperties.
I have implemented the same, with corrections, and using blocks and posted below:
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;
}];

How about this:
// Application documents directory
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:documentsURL
includingPropertiesForKeys:#[NSURLContentModificationDateKey]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSArray *sortedContent = [directoryContent sortedArrayUsingComparator:
^(NSURL *file1, NSURL *file2)
{
// compare
NSDate *file1Date;
[file1 getResourceValue:&file1Date forKey:NSURLContentModificationDateKey error:nil];
NSDate *file2Date;
[file2 getResourceValue:&file2Date forKey:NSURLContentModificationDateKey error:nil];
// Ascending:
return [file1Date compare: file2Date];
// Descending:
//return [file2Date compare: file1Date];
}];

Simpler...
NSArray* filelist_sorted;
filelist_sorted = [filelist_raw sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary* first_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:#"%#/%#", path_thumb, obj1] error:nil];
NSDate* first = [first_properties objectForKey:NSFileModificationDate];
NSDictionary* second_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:#"%#/%#", path_thumb, obj2] error:nil];
NSDate* second = [second_properties objectForKey:NSFileModificationDate];
return [second compare:first];
}];

It's too slow
[[NSFileManager defaultManager]
attributesOfItemAtPath:NSFileModificationDate
error:&error];
Try this code:
+ (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;
}

Code does not work in iPhone SDK, full of compilation error. Please find updated code
`
NSInteger lastModifiedSort(id path1, id path2, void* context)
{
int comp = [[path1 objectForKey:#"lastModDate"] compare:
[path2 objectForKey:#"lastModDate"]];
return comp;
}
-(NSArray *)filesByModDate:(NSString*) path{
NSError* error = nil;
NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
error:&error];
if(error == nil)
{
NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];
for(NSString* imgName in filesArray)
{
NSString *imgPath = [NSString stringWithFormat:#"%#/%#",path,imgName];
NSDictionary* properties = [[NSFileManager defaultManager]
attributesOfItemAtPath:imgPath
error:&error];
NSDate* modDate = [properties objectForKey:NSFileModificationDate];
if(error == nil)
{
[filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
imgName, #"path",
modDate, #"lastModDate",
nil]];
}else{
NSLog(#"%#",[error description]);
}
}
NSArray* sortedFiles = [filesAndProperties sortedArrayUsingFunction:&lastModifiedSort context:nil];
NSLog(#"sortedFiles: %#", sortedFiles);
return sortedFiles;
}
else
{
NSLog(#"Encountered error while accessing contents of %#: %#", path, error);
}
return filesArray;
}
`

Related

Some strange error occurs when manipulating events in the calendar of iOS device

I added some events to the calendar and save their eventIdentifier to file. When i want to remove all my events i read the eventIdentifier from that file to an array and remove each event with its event id. Here is the code to add event to calendar and save their event id to file:
- (void) addEventToCalendar: (id)object
{
#autoreleasepool {
int i = 0;
NSString *string_to_file = #"";
eventStore=[[EKEventStore alloc] init];
for(Schedule *sche in scheduleArray){
EKEvent *addEvent=[EKEvent eventWithEventStore:eventStore];
addEvent.title=sche.course_Name;
addEvent.startDate = [self stringToDate:sche.from_Date];
addEvent.endDate = [self stringToDate:sche.thru_Date];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[addEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
NSDate *date_alarm = [addEvent.startDate dateByAddingTimeInterval:-(10*60)];
addEvent.alarms=[NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:date_alarm]];
NSError *err;
// do save event to calendar
[eventStore saveEvent:addEvent span:EKSpanThisEvent error:&err];
if (err == nil) {
NSString* str = [[NSString alloc] initWithFormat:#"%#", addEvent.eventIdentifier];
string_to_file = [string_to_file stringByAppendingString:str];
string_to_file = [string_to_file stringByAppendingString:#"\n"];
NSLog(#"String %d: %#",i, str);
}
else {
NSLog(#"Error %#",err);
}
i++;
}
// create file to save
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
inFile = [NSFileHandle fileHandleForWritingAtPath: filePath];
NSData *data = [string_to_file dataUsingEncoding:NSUTF16StringEncoding];
[inFile writeData:data];
}
}
And the code below to remove all events i have added to calendar
- (void) deleteEventInCalender {
filemgr = [NSFileManager defaultManager];
NSString *filePath = [self getFilePath:#"saveeventid.txt"];
NSFileHandle *inFile;
inFile = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *dataFile;
dataFile = [inFile readDataToEndOfFile];
NSString *tmp = #"";
NSString *temp = #"";
tmp = [NSString stringWithCharacters:[dataFile bytes] length:[dataFile length]/sizeof(unichar)];
if(![tmp isEqualToString:#""]){
tmp = [tmp substringFromIndex:1];
event_idArray = [[NSMutableArray alloc] init];
int j = 0;
while (![tmp isEqualToString:#""]){
int index_find_string = [tmp rangeOfString:#"\n"].location;
temp = [tmp substringWithRange:NSMakeRange(0, index_find_string)];
[event_idArray addObject:temp];
tmp = [tmp substringFromIndex:index_find_string + 1];
}
EKEventStore* store = [[EKEventStore alloc] init];
j = 0;
for(NSString *eventid in event_idArray){
EKEvent* event2 = [store eventWithIdentifier:eventid];
if (event2 != nil) {
NSLog(#"log: %d log id: %#", j, eventid);
NSError* error = nil;
// remove event
[store removeEvent:event2 span:EKSpanThisEvent error:&error];
}
j++;
}
[filemgr removeItemAtPath:filePath error:nil];
}
}
All codes above work well when i test on the iOS simulator with calendar.sqlitedb. But it makes some strange errors when i run on iPad device 5.0. That is sometime the calendar not remove event or when all events has been remove then after some minutes all events appear again... I don't understand, i don't know why and i very confuse. Does anyone has the same issue with me? Please share your solution!
Added another question: where the calendar database stored in the iOS 5.0 device.

How to get name and creation date of all files in a folder in ios?

I am using the following code to get creation date of a file at a given path and it works fine.
NSDictionary* attrs = [fm attributesOfItemAtPath:#"/Users/me/Desktop/ios" error:nil];
if (attrs != nil) {
NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
NSLog(#"Date Created: %#", [date description]);
}
else {
NSLog(#"Not found");
}
Now I want to get the names and creation dates of all files in a folder given at a path.How can I do that? Can anybody help me?
first get list of all files from your directory & then loop until all file completes
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
for(int i=0;i<[dirContents count];i++)
{
NSDictionary* attrs = [fm attributesOfItemAtPath:[dirContents objectAtIndex:i] error:nil];
if (attrs != nil) {
NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
NSLog(#"Date Created: %#", [date description]);
}
else {
NSLog(#"Not found");
}
}

How to delete files from iPhone's document directory which are older more than two days

I have an App in which I record the sound files and store it in apps Document Directory.
What I want is, it should contain only yesterday and to days older files and remove all others from the folder in iPhone app. Is there any way to do this?
Thanks ..
Please look at 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);
}
}
}
If you would have searched little you would have got this:
[fileManager removeItemAtPath: fullPath error:NULL];
So you can get 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 those date in if condition and delete those files.
UPDATE 1: Working code to delete files which are 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];
}
}
}
Please check following code for swift :
func cleanUp() {
let maximumDays = 2.0
let minimumDate = Date().addingTimeInterval(-maximumDays*24*60*60)
func meetsRequirement(date: Date) -> Bool { return date < minimumDate }
func meetsRequirement(name: String) -> Bool { return name.hasPrefix(applicationName) && name.hasSuffix("log") }
do {
let manager = FileManager.default
let documentDirUrl = try manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
if manager.changeCurrentDirectoryPath(documentDirUrl.path) {
for file in try manager.contentsOfDirectory(atPath: ".") {
let creationDate = try manager.attributesOfItem(atPath: file)[FileAttributeKey.creationDate] as! Date
if meetsRequirement(name: file) && meetsRequirement(date: creationDate) {
try manager.removeItem(atPath: file)
}
}
}
}
catch {
print("Cannot cleanup files: \(error)")
}
}
Please check 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];
}
}

Save text in plist file with specific date

I am trying to store some stings into a plist file, the saving process works fine, but these notes should be saved based on a specific date, for example I enter a note on 2 Feb then I need enter another note on 5 Feb, and when I move during these dates my notes should show on these dates. I would be grateful if you help me.
Here is my code:
//Save Setting ///////////////////
- (NSString *) saveFilePath
{
NSArray *pathArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDate *date = [NSDate date];
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:#"note.plist"];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
NSArray *values = [[NSArray alloc] initWithObjects:saveTextToday.text ,nil];
[values writeToFile:[self saveFilePath] atomically:YES];
[values release];
}
- (void)viewDidLoad
{
////Save Setting /////////////////////////////////////////////////
NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if (fileExists)
{
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
saveTextToday.text = [values objectAtIndex:0];
[values release];
}
// notification
UIApplication *myApp = [UIApplication sharedApplication];
// add yourself to the dispatch table
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:myApp];
}
It appears you're saving an array to a file, but using a ".plist" file extension. While the file created will be a plist file, there is no date information in the content.
Instead of an array, you could use an NSDictionary. Use the current date as the key, and the array you're using now as the object. Like this:
NSMutableDictionary *myDictionary = [NSMutableDictionary alloc] initWithContentsOfFile:myPath];
NSString *todayKey = [self showPersianFullDate]; // or [self showPersianFullDate:[NSDate date]]
[myDictionary setObject:saveTextToday.text forKey:todayKey];
[myDictionary writeToFile:[self saveFilePath] atomically:YES];
I think what you want to do is use the NSFileManager to find out when the file was saved, like this:
NSString *path = #"the path you've saved you plist to";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDate *creationDate = nil;
if ([fileManager fileExistsAtPath:path])
{
NSError *error = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:&error];
creationDate = [attributes objectForKey:NSFileCreationDate];
}
creationDate now contains the date when the file was first saved. To get the date when it was last saved, use NSFileModificationDate instead.

How to sort files by modified date in iOS

I used NSFileManager to retrieve the files in a folder, and I want to sort them by modified date. How to do that ?
Thanks.
What have you tried so far?
I haven't done this, but a quick look at the docs makes me think that you should try the following:
Call -contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: and specify NSURLContentModificationDateKey as one of the keys.
You'll get back an array of NSURL objects which you can then sort using an NSArray method like -sortedArrayUsingComparator:.
Pass in a comparator block that looks up the modification date for each NSURL using -getResourceValue:forKey:error:.
Update: When I wrote the answer above, -getResourceValue:forKey:error: existed in iOS but didn't do anything. That method is now functional as of iOS 5. The following code will log an app's resource files followed by a list of corresponding modification dates:
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *files = [manager contentsOfDirectoryAtURL:[[NSBundle mainBundle] resourceURL]
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLContentModificationDateKey]
options:nil
error:nil];
NSMutableArray *dates = [NSMutableArray array];
for (NSURL *f in files) {
NSDate *d = nil;
if ([f getResourceValue:&d forKey:NSURLContentModificationDateKey error:nil]) {
[dates addObject:d];
}
}
NSLog(#"Files: %#", files);
NSLog(#"Dates: %#", dates);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *originalImage = [[NSMutableArray alloc]init];
for (int i = 1; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:#"%#/%#",documentsDirectory,[imageFilenames objectAtIndex:i] ];
}
//---------sorting image by date modified
NSArray* filelist_date_sorted;
filelist_date_sorted = [imageFilenames sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSDictionary* first_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:#"%#/%#", documentsDirectory, obj1] error:nil];
NSDate *first = [first_properties objectForKey:NSFileCreationDate];
NSDictionary *second_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:#"%#/%#", documentsDirectory, obj2] error:nil];
NSDate *second = [second_properties objectForKey:NSFileCreationDate];
return [second compare:first];
}];
NSLog(#" Date sorted result is %#",filelist_date_sorted);
static static NSInteger contentsOfDirSort(NSString *left, NSString *right, void *ptr) {
(void)ptr;
struct stat finfo_l, r_finfo_r;
if(-1 == stat([left UTF8String], &finfo_l))
return NSOrderedSame;
if(-1 == stat([right UTF8String], &finfo_r))
return NSOrderedSame;
if(finfo_l.st_mtime < finfo_r.st_mtime)
return NSOrderedAscending;
if(finfo_l.st_mtime > finfo_r.st_mtime)
return NSOrderedDescending;
return NSOrderedSame;
}
Now, later on in your code.
NSMutableArray *mary = [NSMutableArray arrayWithArray:filePathsArray];
[mary sortUsingFunction:contentsOfDirSort context:nil];
// Use mary...
Quick Method:
-(void)dateModified
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
//--- Listing file by name sort
NSLog(#"\n File list %#",fileList);
int num;
//-- Listing file name with modified dated
for (NSString *s in fileList)
{
NSString *filestring = [documentsDirectory stringByAppendingFormat:#"/%#",s];
NSDictionary *filePathsArray1 = [[NSFileManager defaultManager] attributesOfItemAtPath:filestring error:nil];
NSString *modifiedDate = [filePathsArray1 objectForKey:NSFileModificationDate];
NSLog(#"\n Modified Day : %#", modifiedDate);
num=num+1;
}
}
In a category over NSFileManager:
static NSInteger contentsOfDirSort(NSURL *leftURL, NSURL *rightURL, void *ptr)
{
(void)ptr;
NSDate * dateLeft ;
[leftURL getResourceValue:&dateLeft
forKey:NSURLContentModificationDateKey
error:nil] ;
NSDate * dateRight ;
[rightURL getResourceValue:&dateRight
forKey:NSURLContentModificationDateKey
error:nil] ;
return [dateLeft compare:dateRight];
}
- (NSArray *)contentsOrderedByDateOfDirectoryAtPath:(NSURL *)URLOfFolder ;
{
NSArray *files = [self contentsOfDirectoryAtURL:URLOfFolder
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLContentModificationDateKey]
options:0
error:nil];
return [files sortedArrayUsingFunction:contentsOfDirSort
context:nil] ;
}