Re: File Size Code
Re: File Size Code
- Subject: Re: File Size Code
- From: Scott Ahten <email@hidden>
- Date: Sun, 9 May 2004 03:11:54 -0400
If you want to do a deep directory scan at a specific path, you can get
the attributes of each file from the enumerator. Here's an example...
---
NSFileManager * fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *enumerator = [fileManager
enumeratorAtPath:@"/Applications/Applescript/"];
int totalBytes = 0;
NSDictionary *fattrs;
NSNumber *fsize;
while (file = [enumerator nextObject]) {
NSLog(file);
fattrs = [enumerator fileAttributes];
// show all available attributes for the file
NSLog([fattrs description]);
if ([fattrs objectForKey:NSFileType] == NSFileTypeRegular) {
if (fsize = [fattrs objectForKey:NSFileSize]) {
NSLog(@"File size: %d\n", [fsize intValue]);
totalBytes+= [fsize intValue];
}
}
}
NSLog(@"Total Bytes: %d\n", totalBytes);
---
This should add up the total number of bytes for all files in the path
"/Applications/Applescript/", excluding directories.
On May 9, 2004, at 12:40 AM, email@hidden wrote:
ok here's my code to add the file size of files/directories
- (void) queryFileSize;
{
NSFileManager *manager = [NSFileManager defaultManager];
NSDictionary *rule = [NSDictionary
dictionaryWithDictionary:[itemArray objectAtIndex:[backupTable
selectedRow]]];
NSArray *pathArray = [[rule objectForKey:@"path"]
componentsSeparatedByString:@":"];
NSEnumerator *e = [pathArray objectEnumerator];
id object;
int total = 0;
BOOL isDir;
while(object = [e nextObject])
{
if ([manager fileExistsAtPath:[object
stringByExpandingTildeInPath] isDirectory:&isDir] && isDir)
{
NSString *file;
NSDirectoryEnumerator *enumerator = [[NSFileManager
defaultManager] enumeratorAtPath:[object
stringByExpandingTildeInPath]];
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]
init];
while (file = [enumerator nextObject])
{
NSDictionary *fileSize = [manager
fileAttributesAtPath:file traverseLink:NO];
NSNumber *totalFileSize = [fileSize
objectForKey:NSFileSize];
total += [totalFileSize intValue];
}
//[pool release];
}
else
{
NSDictionary *fileSize = [manager
fileAttributesAtPath:[object stringByExpandingTildeInPath]
traverseLink:NO];
NSNumber *totalFileSize = [fileSize
objectForKey:NSFileSize];
total += [totalFileSize intValue];
}
}
NSLog(@"%d",total);
}
my problem is, its not reading the size for some reason it will on 1
file and then the rest come up as zero?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
- - - - -
:: email@hidden
::
http://www.pixelfreak.net
- - - - -
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.