简体   繁体   中英

Get File properties of files stored in folders in iPhone

I have a very simple code to get the list of all files that are stored in my CacheFolder in iPhone

 NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
    NSString *documentsPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:APP_NAME];

    NSError *error;
    NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

However, instead of name only, i want to get each file size and the creation date. Is that possible somehow?

NSError *attributesError;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:documentsPath error:&attributesError];

NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
NSDate *creationDate = (NSDate *)[fileAttributes objectForKey:NSFileCreationDate];

You can also get the following attributes:

NSString * const  NSFileType;
NSString * const  NSFileSize;
NSString * const  NSFileModificationDate;
NSString * const  NSFileReferenceCount;
NSString * const  NSFileDeviceIdentifier;
NSString * const  NSFileOwnerAccountName;
NSString * const  NSFileGroupOwnerAccountName;
NSString * const  NSFilePosixPermissions ;
NSString * const  NSFileSystemNumber;
NSString * const  NSFileSystemFileNumber;
NSString * const  NSFileExtensionHidden;
NSString * const  NSFileHFSCreatorCode;
NSString * const  NSFileHFSTypeCode;
NSString * const  NSFileImmutable;
NSString * const  NSFileAppendOnly;
NSString * const  NSFileCreationDate;
NSString * const  NSFileOwnerAccountID;
NSString * const  NSFileGroupOwnerAccountID;
NSString * const  NSFileBusy;

您想要的是NSFileManager的attributesOfItemAtPath:error API ,它将返回一个字典,其中包含NSFileSizeNSFileCreationDate类的属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM