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