Nsfilemanager Calculate File/Folder size

Source: Internet
Author: User

In some software, the user will generally show the memory of the current app, while the user can according to their own needs to clear the cached content. In general, the folder is not the size of this property, so you need to traverse the contents of the folder to calculate the size of the folder, the following with Nsfilemanger to achieve this function.

Learn that the file/folder path is a nsstring string type, so you can add a taxonomy to a string if it is a file/folder implementation that calculates its size. The classification is named FileSize.

1-(Nsinteger) filesize{2     //File Manager3Nsfilemanager *mgr =[Nsfilemanager Defaultmanager];4     //determine if a string is a file/folder5BOOL dir =NO;6BOOL exists = [Mgr Fileexistsatpath:self isdirctory:&dir];7     //File/folder does not exist8     if(exists = = NO)return 0;9     //Self is a folderTen     if(dir) { One          //traverse all content in a folder ANsarray *subpaths =[Mgr Subpathsatpath:self]; -          //Calculate Folder Size -Nsinteger totalbytesize =0; the           for(NSString *subpathinchsubpaths) { -               //Stitching Full path -NSString *fullsubpath =[self stringbyappendingpathcomponent:subpath]; -             //determine if the file is +BOOL dir =NO; -[Mgr Fileexistsatpath:fullsubpath isdirectory:&dir]; +             if(dir = = NO) {//is a file ANsdictionary *attr =[Mgr Attributesoffitematpath:fullsubpath error:ni]; atTotalbytesize + =[attr[nsfilesize] integervalue]; -              } -          } -          returntotalbytesize;  -}Else{//is a file -Nsdictionary *attr =[Mgr Attributesoffitematpath:self error:ni]; in            return[attr[nsfilesize] integervalue]; -    }    to}

This allows for the calculation of file/folder size

For example, calculate the size of the caches file

1 nsstring *caches =[[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) Lastobject]; 2 Integer cachessize = [caches fileSize];

This will give you the size of the caches folder.

If the input string is not a file/folder, get 0.

Nsfilemanager Calculate File/Folder size

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.