2

I'm new in objective-c. I have a path to file contained in an NSString and I want get file size. I found this example and change deprecated code with attributesOfItemAtPath:error: but path is always invalid.

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *path = @"~/Library/Safari/History.plist";
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath: path error: NULL];


if (fileAttributes != nil) {
    NSNumber *fileSize;

    if (fileSize == [fileAttributes objectForKey:NSFileSize]) {
        NSLog(@"File size: %qi\n", [fileSize unsignedLongLongValue]);
    }

}
else {
    NSLog(@"Path (%@) is invalid.", pPath);
}
[NSFileManager release];
Joannes
  • 81
  • 1
  • 11

5 Answers5

4

This should work:

uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil] fileSize];

It's very similar to the one used by you, but in yours there's a mistake: you put NULL instead of nil in the error: handling.

Make sure also to expand tilde in your path, as explained in the documentation: use stringByExpandingTildeInPath, so your NSString *path should be something like this:

NSString *path = [[NSString stringWithString:@"~/Library/Safari/History.plist"] stringByExpandingTildeInPath];

Here you can find some explanations about the difference between nil and NULL.

Community
  • 1
  • 1
Sylter
  • 1,622
  • 13
  • 26
  • I think the problem is not tilde (~), with NSString *path = @"Whatever.txt"; the code don't work. – Joannes Jan 02 '12 at 10:07
  • Have you tried with `nil` instead of `NULL` in error handling? The code I've posted is tested and used in some of my applications, so I'm sure it works. Try with the **full path**, e.g. **/Users/USERNAME/Library/Safari/History.plist** and use the `defaultMangaer` of `NSFileManager ` instead creating your own instance. [Here (http://pastebin.com/XBBCvqGu)](http://pastebin.com/XBBCvqGu) I wrote a piece of code that does what you're looking for: it's tested and it works. – Sylter Jan 02 '12 at 11:04
  • I tried nil and there isn't error but the code don't work. In console I tried your code and it's ok, but can I need to enter your user name. how can I not put it? only /Library/Safari/History.plist? – Joannes Jan 02 '12 at 12:21
  • [Here (http://pastebin.com/DCMWwnXK)](http://pastebin.com/DCMWwnXK) you find the code without needing to put the username. – Sylter Jan 02 '12 at 14:01
1

You might need to expand the path using:

 - (NSString *)stringByExpandingTildeInPath
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • This method is under-rated. I love it :) Much simpler and more readable than stringWithFormat and NSHomeDirectory(). – Chris Nolet Jul 21 '13 at 03:10
1

you can get size by :

NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:yourFilePath error:nil];
NSNumber * size = [properties objectForKey: NSFileSize];

size is a NSNumber that contains a unsigned long long.

Maulik
  • 19,348
  • 14
  • 82
  • 137
1

Your Path will always be invalid because of a super-silly bug in your code.

Change

if (fileSize == [fileAttributes objectForKey:NSFileSize]) {

to

if (fileSize = [fileAttributes objectForKey:NSFileSize]) {

I hope no further explanatiuon would be required.

Raunak
  • 3,314
  • 1
  • 22
  • 28
0

Use the defaultManager class method on NSFileManager instead of creating your own instance. Also, do not include ~ (tilde) symbol in your file path. Use the NSHomeDirectory() function to get the home directory instead. Here's an example:

NSString *path = [NSString stringWithFormat:@"%@/Library/Safari/History.plist", NSHomeDirectory()];
[[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] fileSize];

This should return the size of your file.

Chris Ledet
  • 11,458
  • 7
  • 39
  • 47
  • without NSDictionary *fileAttributes the rest of the code does not work. Ok without ~ using for example NSString *path = @"Whatever.txt"; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath: path error: NULL]; the code don't work again. – Joannes Jan 02 '12 at 10:16