Sina Weibo Client (62)-Calculate the size of a file or folder

Source: Internet
Author: User

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

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.