IOS obtains the file size.

Source: Internet
Author: User
Tags lstat

Folder:

First import the header file:

#include <sys/stat.h>#include <dirent.h>#define Localizable_LF_Size_Bytes                                   @"%lld Bytes"#define Localizable_LF_Size_K                                       @"%lld K"#define Localizable_LF_Size_M                                       @"%lld.%lld M"#define Localizable_LF_Size_G                                       @"%lld.%d G"#define Localizable_LF_All_Size_M                                   @"%lld.%lld M"#define Localizable_LF_All_Size_G                                   @"%lld.%lld G"

-(Long) foldersizeatpath :( nsstring *) folderpath {return [self _ foldersizeatpath: [folderpath cstringusingencoding: Hangzhou];}-(long) _ foldersizeatpath: (const char *) folderpath {long foldersize = 0; dir * dir = opendir (folderpath); If (DIR = NULL) return 0; struct dirent * child; while (child = readdir (DIR ))! = NULL) {If (child-> d_type = dt_dir & (child-> d_name [0] = '. '& Child-> d_name [1] = 0) | // ignore the directory. (child-> d_name [0] = '. '& Child-> d_name [1] = '. '& Child-> d_name [2] = 0) // ignore directory ..)) continue; int folderpathlength = strlen (folderpath); char childpath [1024]; // path address of the sub-file stpcpy (childpath, folderpath); If (folderpath [folderPathLength-1]! = '/') {Childpath [folderpathlength] = '/'; folderpathlength ++;} stpcpy (childpath + folderpathlength, child-> d_name ); childpath [folderpathlength + child-> d_namlen] = 0; If (child-> d_type = dt_dir) {// directory foldersize + = [self _ foldersizeatpath: childpath]; // recursively call the subdirectory // Add the space occupied by the Directory to struct stat st; If (lstat (childpath, & St) = 0) foldersize + = ST. st_size;} else if (child-> d_type = dt_reg | child-> d_type = dt_lnk) {// file or link struct stat st; If (lstat (childpath, & St) = 0) foldersize + = ST. st_size ;}} return foldersize ;} /*************************************** * ************************************ Function name: + (nsstring *) stringforallfilesize :( uint64) filesize Function Description: Format: Return file size input parameter: (uint64) filesize output parameter: N/A return parameter: (nsstring *) remarks: **************************************** **************************************/- (nsstring *) stringforallfilesize :( uint64) filesize {If (filesize <1024) {// Bytes/byte if (filesize> 1) {return [nsstring stringwithformat: localizable_lf_size_bytes, filesize];} else {// = 1 byte return [nsstring stringwithformat: localizable_lf_size_bytes, filesize] ;}} if (1024*1024)> (filesize) & (filesize)> 1024) {// K return [nsstring stringwithformat: localizable_lf_size_k, filesize/1024];} If (1024*1024*1024)> filesize & filesize> (1024*1024 )) {// M return [nsstring stringwithformat: localizable_lf_all_size_m, filesize/(1024*1024), filesize % (1024*1024)/(1024*102)];} if (filesize> (1024*1024*1024) {// G return [nsstring stringwithformat: localizable_lf_all_size_g, filesize/(1024*1024*1024 ), filesize % (1024*1024*1024)/(1024*1024*102)];} return nil ;}

File Size:

            long sizelong = [[[manager attributesOfFileSystemForPath:cacheDocPath error:nil] objectForKey:NSFileSize] longLongValue];

Reference: http://www.easymorse.com/index.php/archives/638

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.