IOS Nsfilemaneger calculates whether a file timed out, and calculates the total size of a file under a folder
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 16px;">Gets the specified file distance from the last modified time to the specified value (in seconds) timeout</span></p></p><p><p><span style="font-size: 16px;">+ (BOOL) istimeout: (nsstring *) path time: (nstimeinterval) timeout</span></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p><span style="font-size: 16px;">Get current time</span></p></p><p><p><span style="font-size: 16px;">Nstimeinterval now = [[nsdate date] timeIntervalSince1970];</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">Nsdictionary *dict = [[nsfilemanager defaultmanager] attributesofitematpath:path error:nil];</span></p></p><p><p><span style="font-size: 16px;">The time that the file was last modified was obtained</span></p></p><p><p><span style="font-size: 16px;">NSDate *d = [dict objectforkey:nsfilemodificationdate];</span></p></p><p><p><span style="font-size: 16px;">If (now-[d Timeintervalsince1970]>timeout) {</span></p></p><p><p><span style="font-size: 16px;">Return YES;</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">Return NO;</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">Calculate the total size of a file under a folder</span></p></p><p><p><span style="font-size: 16px;">+ (float) filesizefordir: (nsstring*) path</span></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p><span style="font-size: 16px;">Nsfilemanager *filemanager = [nsfilemanager defaultmanager];</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">Record value</span></p></p><p><p><span style="font-size: 16px;">unsigned long long totalsize = 0;</span></p></p><p><p><span style="font-size: 16px;">Get all the contents (files and Folders) of the path specified</span></p></p><p><p><span style="font-size: 16px;">nsarray* array = [filemanager Contentsofdirectoryatpath:path error:nil];</span></p></p><p><p><span style="font-size: 16px;">for (int i = 0; i<[array count]; i++)</span></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p><span style="font-size: 16px;">Stitching full path</span></p></p><p><p><span style="font-size: 16px;">NSString *fullpath = [path Stringbyappendingpathcomponent:[array objectatindex:i];</span></p></p><p><p><span style="font-size: 16px;">BOOL isdir;</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p><span style="font-size: 16px;">If the specified path exists and is not a folder</span></p></p><p><p><span style="font-size: 16px;">NSLog (@ "fullpath:%@", fullPath);</span></p></p><p><p><span style="font-size: 16px;">Determine if it exists, and then decide whether it is a folder or a file</span></p></p><p><p><span style="font-size: 16px;">If ([filemanager fileexistsatpath:fullpath isdirectory:&isdir] &&!isdir)</span></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p><span style="font-size: 16px;">Get file attributes</span></p></p><p><p><span style="font-size: 16px;">Nsdictionary *fileattributedic=[filemanager Attributesofitematpath:fullpath error:nil];</span></p></p><p><p><span style="font-size: 16px;">Totalsize+=[[fileattributedic objectforkey:nsfilesize] unsignedlonglongvalue];</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">Else</span></p></p><p><p><span style="font-size: 16px;">{</span></p></p><p><p><span style="font-size: 16px;">If it is a folder, recursion</span></p></p><p><p><span style="font-size: 16px;">Totalsize+=[self filesizefordir:fullpath];</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span style="font-size: 16px;">Return totalsize;</span></p></p><p><p><span style="font-size: 16px;">}</span></p></p><p><p><span></span><br></p></p><p><p><br></p></p><p><p>IOS Nsfilemaneger calculates whether a file timed out, and calculates the total size of a file under a folder</p></p></span>
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