Enumerating a folder sorted by mod time
Enumerating a folder sorted by mod time
- Subject: Enumerating a folder sorted by mod time
- From: email@hidden
- Date: Sun, 21 Aug 2005 18:31:55 +0000
Hi all! I am sure what I want to do is simple, but for some reason I just can't figure it out. What I want to do is, enumerate all of the files in a particular folder that have a particular string in their name. That is not a problem. Then I want to take that list of files and sort them by modification time. This is what I tried:
NSArray *directoryContents = [[[NSArray alloc] initWithArray:[fileManager directoryContentsAtPath:completeSavePath]] autorelease];
int numberOfFiles = [directoryContents count];
int i = 0;
int modificationDatesAdded = 0;
NSArray *fileArray = [[[NSArray alloc] initWithObjects:nil] autorelease];
for (i = 0; i < numberOfFiles; i++)
{
//Check if this object has the word packet in it
NSRange packetStringRange = [[directoryContents objectAtIndex:i] rangeOfString:@"packet"];
//If it does
if (packetStringRange.length != 0)
{
//Create the complete path
NSString *completePath = [[[NSString alloc] initWithString:completeSavePath] autorelease];
completePath = [completePath stringByAppendingPathComponent:[directoryContents objectAtIndex:i]];
NSDictionary *fattrs = [fileManager fileAttributesAtPath:completePath traverseLink:NO];
NSDate *moddate;
//Add the modification date to the array
if (moddate = [fattrs objectForKey:NSFileModificationDate])
{
fileArray = [fileArray arrayByAddingObject:fattrs];
modificationDatesAdded++;
}
}
}
//Now we want to sort the array
if (modificationDatesAdded != 0)
{
NSArray *sortedArray;
NSSortDescriptor *modificationDateDescriptor =[[[NSSortDescriptor alloc] initWithKey:NSFileModificationDate ascending:YES] autorelease];
NSArray *sortDescriptors=[NSArray arrayWithObject:modificationDateDescriptor];
sortedArray= [[[NSArray alloc] initWithArray:[fileArray sortedArrayUsingDescriptors:sortDescriptors]] autorelease];
}
I have an array sorted by modification times, but I not longer have the paths of the files. How can I do this so when I am done I have both the sorted modification times and their matching file paths.
Thanks,
Kris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden