Re: Counting the number of files in a directory
Re: Counting the number of files in a directory
- Subject: Re: Counting the number of files in a directory
- From: Ken Tozier <email@hidden>
- Date: Tue, 6 Mar 2007 00:04:54 -0500
You could make a reusable category on NSFileManager like this
@interface NSFileManager (CatInfo)
+ (int) filesInDirectory:(NSString *) inPath;
@end
@implementation NSFileManager (CatInfo)
+ (int) filesInDirectory:(NSString *) inPath
{
FSRef ref;
Boolean isDir;
FSCatalogInfo catInfo;
const char *pathString = [inPath UTF8String];
OSStatus status = FSPathMakeRef(pathString, &ref, &isDir);
if (status == noErr)
{
if (isDir)
{
OSErr err = FSGetCatalogInfo(&ref, kFSCatInfoValence,
&catInfo, NULL, NULL, NULL);
if (err == noErr)
return catInfo.valence;
else
NSLog(@"get cat info error: %hi", err);
}
else
NSLog(@"not a directory: %@", inPath);
}
else
NSLog(@"err status: %i", status);
return NSNotFound;
}
@end
And use it like this:
int fileCount = [NSFileManager filesInDirectory: @"some dir path"];
NSLog(@"file count: %i", fileCount);
Although it really doesn't have anything to do with an NSFileManager,
creating it as category relates it conceptually
HTH
-Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden