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: Mark Ackerman <email@hidden>
- Date: Sun, 21 Aug 2005 17:07:50 -0400
Kris,
This might help. Its a category on NSFileManager that will give you
an array containing the filtered contents of a directory, sorted by
modification dates. I'm sure you can modify it to suit your exact
needs. (Tested superficially, clumsy method/function names, no error
checking, some errors may have been introduced in copy/paste, etc.,
etc.)
(Just saw Matt Neuberg's response, which well may be more elegant).
#import <Foundation/NSFileManager.h>
@interface NSFileManager (MYFileManagerAdditions)
// returns filenames in directory at path that contain filter, sorted
by modification date
- (NSArray *)MY_directoryContentsAtPath:(NSString *)path
sortedByModificationDateAndFiltered:(NSString *)filter
// utility function that compares modification dates of thisFile and
otherFile in directory fileDir
NSComparisonResult MY_compareByModificationDate(NSString *thisFile,
NSString *otherFile, void *fileDir);
@end
@implementation NSFileManager (MYFileManagerAdditions)
- (NSArray *)MY_directoryContentsAtPath:(NSString *)path
sortedByModificationDateAndFiltered:(NSString *)filter
{
NSMutableArray *filteredFiles = [NSMutableArray array];
NSEnumerator *fileEnumerator = [self enumeratorAtPath:path];
NSString *file = nil;
while (file = [fileEnumerator nextObject])
{
if (NSNotFound != [file rangeOfString:filter].location)
{
[filteredFiles addObject:file];
}
}
NSArray *sortedFilteredFiles = [filteredFiles
sortedArrayUsingFunction:MY_compareByModificationDate context:(void*)
path];
return sortedFilteredFiles;
}
NSComparisonResult MY_compareByModificationDate(NSString *thisFile,
NSString *otherFile, void *fileDir)
{
NSComparisonResult result = NSOrderedSame;
NSString *fileDirPath = (NSString *)fileDir;
NSFileManager *fm = [NSFileManager defaultManager];
NSString *thisPath = [[fileDirPath
stringByAppendingPathComponent:thisFile] stringByExpandingTildeInPath];
NSDictionary *thisAttr = [fm fileAttributesAtPath:thisPath
traverseLink:YES];
NSDate *thisModDate = [thisAttr
objectForKey:NSFileModificationDate];
NSString *otherPath = [[fileDirPath
stringByAppendingPathComponent:otherFile] stringByExpandingTildeInPath];
NSDictionary *otherAttr = [fm fileAttributesAtPath:otherPath
traverseLink:YES];
NSDate *otherModDate = [otherAttr
objectForKey:NSFileModificationDate];
result = [thisModDate compare:otherModDate];
return result;
}
@end
Mark
On Aug 21, 2005, at 2:31 PM, email@hidden wrote:
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.
<snip>
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