Nsstring+extension.m
/** * Calculate the file/folder size specified by the current path string * File/Folder size,-1 indicates that the specified file or folder path does not exist, in bytes*/-(Nsinteger) fileSize { Nsfilemanager *mgr = [Nsfilemanager Defaultmanager]; BOOL dir=NO; BOOL exists= [Mgrfileexistsatpath:selfisdirectory:&dir]; if(!exists) { return-1; } if(dir) {//CatalogueNsinteger totalbytesize =0; Nsarray*subpaths = [MgrSubpathsatpath: Self];//Single File path for(NSString *subpathinchsubpaths) { //calculates the file size under the full path and accumulatesTotalbytesize + =[ [Mgr attributesofitematpath: [self stringbyappendingpathcomponent:subpath] error:nil][nsfilesize ] IntegerValue]; } returntotalbytesize; } Else{//file return[[Mgr Attributesofitematpath:self error:nil][nsfilesize] integervalue]; } }
To remove a file or folder:
// Clear Cache -(void) clearcache { *cachepath = [Nssearchpathfordirectoriesindomains ( Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; *mgr = [Nsfilemanager Defaultmanager]; Removeitematpath:cachepath Error:nil]; }
Final effect:
Sina Weibo Client (62)-Calculate the size of a file or folder