Clear the cache, calculate the folder size

Source: Internet
Author: User

#import "ViewController.h"

@interface viewcontroller ()

@end

@implementation Viewcontroller

-(void) viewdidload {

[Super viewdidload];

//

NSLog (@ " cache file size is %@", [NSString stringwithformat:@ "%0.2fm", [Self foldersizeatpath:[nsstring stringwithformat:@ "%@/library/caches/imagecache", Nshomedirectory ()]]);

#pragma mark sandbox:Library ( system files ) Temp ( temp file ) documents ( for App cache )

NSLog(@ "===%@===", [nsstring stringwithformat:@ "%0.2f", [ Self foldersizeatpath:@ "/users/liushuo/desktop/1567pro "]);

// Clear Cache

[self clearcachefrompath:@ "/users/liushuo/desktop/on- line related documents "];

// Here is the directory that corresponds to the cached image of sdwebimage

// can be changed according to the corresponding requirements of the corresponding file directory

additional setup after loading the view, typically from a nib.

}

size of a single file

-(long long) Filesizeatpath: (nsstring*) filepath{

Nsfilemanager* manager = [nsfilemanager defaultmanager];

if ([Manager Fileexistsatpath: FilePath]) {

return [[Manager Attributesofitematpath: FilePath error:nil] fileSize];

}

return 0;

}

Traverse folder to get folder size, return how many M

-(float ) Foldersizeatpath: (nsstring*) folderpath{

Nsfilemanager* manager = [nsfilemanager defaultmanager];

If (![ Manager Fileexistsatpath: FolderPath])

return 0;

// traverse all files in a folder through enumeration traversal

// Create an enumeration walker

nsenumerator *childfilesenumerator = [[Manager Subpathsatpath: folderpath] objectenumerator] ;

// Declare file name, file size first

nsstring* fileName;

long long foldersize = 0;

while ((fileName = [Childfilesenumerator nextobject]) = nil) {

// Get the path of the current traversal file

nsstring* Fileabsolutepath = [FolderPath stringbyappendingpathcomponent: fileName];

// Call the encapsulated method to get a single file size

Foldersize + = [self filesizeatpath: Fileabsolutepath];

}

return foldersize/(1024.0*1024.0); convert to how many M to return

}

#pragma mark clears the cache size print nshomedritiony go to Documents to view the path

-(void) Clearcachefrompath: (nsstring*) path{

// Create a File Manager

Nsfilemanager * manager = [nsfilemanager defaultmanager];

if ([manager Fileexistsatpath:p Ath]) {

// If the file path exists to get all of the files

Nsarray * Filearr = [manager subpathsatpath:p Ath]; Locate the path to all the child files and save them in the array.

// need to convert to full path first

// Delete all sub-files directly

for (int i = 0; i < Filearr. Count; i++) {

nsstring * fileName = filearr[i];

// full path

nsstring * filePath = [path stringbyappendingpathcomponent: fileName];

NSLog(@ "%@", FilePath);

[Manager removeItemAtPath: FilePath error:nil];

}

}

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

//Dispose of any resources, can be recreated.

}

@end

Clear the cache, calculate the 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.