Re: Enumerating a folder sorted by mod time
Re: Enumerating a folder sorted by mod time
- Subject: Re: Enumerating a folder sorted by mod time
- From: Matt Neuburg <email@hidden>
- Date: Sun, 21 Aug 2005 13:39:26 -0700
On Sun, 21 Aug 2005 18:31:55 +0000, email@hidden said:
>
>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.
Make an array of dictionaries where each dictionary holds both a file path
and a file modification date. Now sort the array using your own sort
function that sorts on the file modification date. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
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