Lefora Free Forum
login join
Loading
656 views

How to get the file size given a path?

Page 1
1–2
rookie - member
4 posts

I have a path to file contained in an NSString.  Is there a method to get its file size?

?
288 posts

The "C" library stat does that by filling in a structure with about 9 properties of the file including the size, creation/access/modification times and some other information.  You can call that from Cocoa.

However, as we're in the Cocoa world, we'll do it the cocoa way.  The global object NSFileManager has a method to obtain a dictionary of file attributes.  You can enumerate, or read a property from the dictionary.

void main(int argc, const char* argv[])
{
if ( argc != 2 ) {
NSLog(@"syntax: filesize filename") ;
return ;
}

NSString*      filename    = [[NSString alloc]initWithCString:argv[1]] ;
NSError*       error       = [[NSError alloc]init] ;
NSDictionary*  dictionary  = [[NSFileManager defaultManager] attributesOfItemAtPath:filename error:&error] ;
NSEnumerator*  enumerator  = [dictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
NSLog(@"%@ -> %@",key,[dictionary objectForKey:key]) ;
}

NSLog(@"\n\nfileSize = %d",(long)[dictionary fileSize]) ;

/*
– fileCreationDate
– fileExtensionHidden
– fileGroupOwnerAccountID
– fileGroupOwnerAccountName
– fileHFSCreatorCode
– fileHFSTypeCode
– fileIsAppendOnly
– fileIsImmutable
– fileModificationDate
– fileOwnerAccountID
– fileOwnerAccountName
– filePosixPermissions
– fileSize
– fileSystemFileNumber
– fileSystemNumber
– fileType
*/

}


545 /Users/rmills/Projects/filesize/build/Debug> filesize filesize
2010-01-20 22:37:42.465 filesize[12019:903] NSFileOwnerAccountID -> 501
2010-01-20 22:37:42.465 filesize[12019:903] NSFileHFSTypeCode -> 0
2010-01-20 22:37:42.466 filesize[12019:903] NSFileSystemFileNumber -> 4620468
2010-01-20 22:37:42.466 filesize[12019:903] NSFileExtensionHidden -> 0
2010-01-20 22:37:42.466 filesize[12019:903] NSFileSystemNumber -> 234881026
2010-01-20 22:37:42.467 filesize[12019:903] NSFileSize -> 14944
2010-01-20 22:37:42.467 filesize[12019:903] NSFileGroupOwnerAccountID -> 20
2010-01-20 22:37:42.468 filesize[12019:903] NSFileHFSCreatorCode -> 0
2010-01-20 22:37:42.468 filesize[12019:903] NSFileOwnerAccountName -> rmills
2010-01-20 22:37:42.468 filesize[12019:903] NSFilePosixPermissions -> 493
2010-01-20 22:37:42.469 filesize[12019:903] NSFileCreationDate -> 2010-01-20 22:37:25 -0800
2010-01-20 22:37:42.470 filesize[12019:903] NSFileType -> NSFileTypeRegular
2010-01-20 22:37:42.470 filesize[12019:903] NSFileGroupOwnerAccountName -> staff
2010-01-20 22:37:42.470 filesize[12019:903] NSFileReferenceCount -> 1
2010-01-20 22:37:42.471 filesize[12019:903] NSFileModificationDate -> 2010-01-20 22:37:25 -0800
2010-01-20 22:37:42.472 filesize[12019:903] 

fileSize = 14944

546 /Users/rmills/Projects/filesize/build/Debug> ls -alt filesize 
-rwxr-xr-x  1 rmills  staff  14944 Jan 20 22:37 filesize
547 /Users/rmills/Projects/filesize/build/Debug> 

__________________
Page 1
1–2

Locked Topic


You must be a member to post in this forum

Join Now!