Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding file's size

In my iPhone app I am using the following code to find a file's size. Even though the file exists, I am seeing zero for the size. Can anyone help me? Thanks in advance.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"XML/Extras/Approval.xml"];  NSLog(@"URL:%@",URL); NSError *attributesError = nil; NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];  int fileSize = [fileAttributes fileSize]; 
like image 931
Kiran Avatar asked Apr 21 '11 12:04

Kiran


People also ask

How do I find the size of a shell file?

Getting file size using find command find "/etc/passwd" -printf "%s" find "/etc/passwd" -printf "%s\n" fileName="/etc/hosts" mysize=$(find "$fileName" -printf "%s") printf "File %s size = %d\n" $fileName $mysize echo "${fileName} size is ${mysize} bytes."

How do I know the size of my print?

To view the current print size and/or change it just go to Image — Image Size and make sure it's in inches as shown in the below screenshot. You can change to the print size you want then go to View – Print Size and it will zoom in so you can see how the image will look at actual print size. 6.

How do you determine size of folder?

Go to Windows Explorer and right-click on the file, folder or drive that you're investigating. From the menu that appears, go to Properties. This will show you the total file/drive size. A folder will show you the size in writing, a drive will show you a pie chart to make it easier to see.


1 Answers

Try this;

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];  NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize]; long long fileSize = [fileSizeNumber longLongValue]; 

Note that the fileSize won't necessarily fit in an integer (especially a signed one) although you could certainly drop to a long for iOS as you'll never exceed that in reality. The example uses long long as in my code I have to be compatible with systems with much larger storage available.

like image 162
Roger Avatar answered Oct 14 '22 13:10

Roger