Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the file size given a path?

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

like image 940
hekevintran Avatar asked May 02 '09 19:05

hekevintran


People also ask

What are the functions to get the file size and file path in Java?

Java get file size using File classJava File length() method returns the file size in bytes. The return value is unspecified if this file denotes a directory. So before calling this method to get file size in java, make sure file exists and it's not a directory.

How do I get the size of a file in C++?

To get a file's size in C++ first open the file and seek it to the end. tell() will tell us the current position of the stream, which will be the number of bytes in the file.

How do I get the size of a file in Python?

The python os module has stat() function where we can pass the file name as argument. This function returns a tuple structure that contains the file information. We can then get its st_size property to get the file size in bytes.

How do I find the file size in kb?

Step 2: Multiply total number of pixels by the bit depth of the detector (16 bit, 14 bit etc.) to get the total number of bits of data. Step 3: Dividing the total number of bits by 8 equals the file size in bytes. Step 4: Divide the number of bytes by 1024 to get the file size in kilobytes.


2 Answers

This one liner can help people:

unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize]; 

This returns the file size in Bytes.

like image 158
Oded Ben Dov Avatar answered Sep 22 '22 20:09

Oded Ben Dov


Bear in mind that fileAttributesAtPath:traverseLink: is deprecated as of Mac OS X v10.5. Use attributesOfItemAtPath:error: instead, described at the same URL thesamet mentions.

With the caveat that I'm an Objective-C newbie, and I'm ignoring errors that might occur in calling attributesOfItemAtPath:error:, you can do the following:

NSString *yourPath = @"Whatever.txt"; NSFileManager *man = [NSFileManager defaultManager]; NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL]; UInt32 result = [attrs fileSize]; 
like image 21
Frank Shearar Avatar answered Sep 22 '22 20:09

Frank Shearar