The IOS Compute folder contains the size of all files

Source: Internet
Author: User

The IOS Compute folder contains the size of all files

File Size:


Calculate Output Size:

Method One:

classfunc fileSize (path:nsstring!) -Float64{

let filemanager =nsfilemanager()

var size:Float64 =0.0

do {

let arr = try FileManager. contentsofdirectoryatpath (path as string )

if arr. Count == 0 {return size} // when a folder has no files

for index in0... arr. Count - 1 {

let fullPath = path. stringbyappendingpathcomponent (Arr[index])

var isdir: objcbool = false //isdir

if FileManager. fileexistsatpath (FullPath, isdirectory: &isdir) &&!isd ir { Span style= "COLOR: #1d9421" >// No folder calculated size

let fileattributedic =try! filemanager. Attributesofitematpath (fulllpath)

size + = (fileattributedic[nsfilesize]?. Doublevalue)!

}else {

size + = Filemanagertool . fileSize (FullPath) // calculate its size recursively

}

}

return size

}catch {

return size

}

}


Method Two:    classfuncfileSize2 (path:NSString!) -Float64 {

let filemanager =nsfilemanager()

var size:Float64 =0.0

let arr = filemanager. subpathsatpath As string )

if arr!. Count == 0 {return size} // when a folder has no files

for index in0... arr!. Count - 1 {

let fullPath = path. stringbyappendingpathcomponent (arr![ Index])

var Isdir: objcbool = false //isdir

if filemanager. Fileexistsatpath (FullPath, isdirectory: &isdir) &&!isdir {// not folder calculated size

let fileattributedic =try! filemanager. Attributesofitematpath (Fullpath)

size + = (fileattributedic[nsfilesize]?. Doublevalue)!

}

}

return size

}


Two ways to find a path:Contentsofdirectoryatpath: To get the folder and file path under the current folder in a non-recursive way Subpathsatpath: Recursively get all folders and file paths under the current folder

The IOS Compute folder contains the size of all files

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.