I am trying to get the size of a directory, as well as it's content on OS X using Swift. So far, I have only been able to get the size of the directory itself, with none of it's content. For most of my directories it generally shows a value of 6,148 bytes but it does varie.
I have tried the directorySize() function from the file below but it returned 6,148 bytes as well.
I tried the top 2 answers from this question, but was unsure what argument it needed passed Swift to the Objective-C function. I believe it wants a pointer (I am a beginning programmer in learning).
Calculate the size of a folder
And I could not get the Swift answer from here to work for my purpose either.
How to get the file size given a path?
I am using Xcode 7.0 and running OS X 10.10.5.
update: Xcode 8.2.1 • Swift 3.0.2
// get your directory url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// check if the url is a directory
if (try? documentsDirectoryURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
print("url is a folder url")
// lets get the folder files
var folderSize = 0
(try? FileManager.default.contentsOfDirectory(at: documentsDirectoryURL, includingPropertiesForKeys: nil))?.lazy.forEach {
folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
}
// format it using NSByteCountFormatter to display it properly
let byteCountFormatter = ByteCountFormatter()
byteCountFormatter.allowedUnits = .useBytes
byteCountFormatter.countStyle = .file
let folderSizeToDisplay = byteCountFormatter.string(for: folderSize) ?? ""
print(folderSizeToDisplay) // "X,XXX,XXX bytes"
}
If you would like to include all subFolders, hidden files and package descendants you need to use enumeratorAtURL as follow:
// get your directory url
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// check if the url is a directory
if (try? documentsDirectoryURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
var folderSize = 0
(FileManager.default.enumerator(at: documentsDirectoryURL, includingPropertiesForKeys: nil)?.allObjects as? [URL])?.lazy.forEach {
folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
}
let byteCountFormatter = ByteCountFormatter()
byteCountFormatter.allowedUnits = .useBytes
byteCountFormatter.countStyle = .file
let sizeToDisplay = byteCountFormatter.string(for: folderSize) ?? ""
print(sizeToDisplay) // "X,XXX,XXX bytes"
}
Swift 3 version here:
func findSize(path: String) throws -> UInt64 {
let fullPath = (path as NSString).expandingTildeInPath
let fileAttributes: NSDictionary = try FileManager.default.attributesOfItem(atPath: fullPath) as NSDictionary
if fileAttributes.fileType() == "NSFileTypeRegular" {
return fileAttributes.fileSize()
}
let url = NSURL(fileURLWithPath: fullPath)
guard let directoryEnumerator = FileManager.default.enumerator(at: url as URL, includingPropertiesForKeys: [URLResourceKey.fileSizeKey], options: [.skipsHiddenFiles], errorHandler: nil) else { throw FileErrors.BadEnumeration }
var total: UInt64 = 0
for (index, object) in directoryEnumerator.enumerated() {
guard let fileURL = object as? NSURL else { throw FileErrors.BadResource }
var fileSizeResource: AnyObject?
try fileURL.getResourceValue(&fileSizeResource, forKey: URLResourceKey.fileSizeKey)
guard let fileSize = fileSizeResource as? NSNumber else { continue }
total += fileSize.uint64Value
if index % 1000 == 0 {
print(".", terminator: "")
}
}
if total < 1048576 {
total = 1
}
else
{
total = UInt64(total / 1048576)
}
return total
}
enum FileErrors : ErrorType {
case BadEnumeration
case BadResource
}
Output value is megabyte. Converted from source: https://gist.github.com/rayfix/66b0a822648c87326645
Swift 3 version
private func sizeToPrettyString(size: UInt64) -> String {
let byteCountFormatter = ByteCountFormatter()
byteCountFormatter.allowedUnits = .useMB
byteCountFormatter.countStyle = .file
let folderSizeToDisplay = byteCountFormatter.string(fromByteCount: Int64(size))
return folderSizeToDisplay
}
来源:https://stackoverflow.com/questions/32814535/how-to-get-directory-size-with-swift-on-os-x