Question
Why is it showing that Developer
is the only directory that contains anything? There are directories like System
, Library
, and Applications
which I know are not 0
.
Code
print(ViewController.getSizeOfFolders(startingAt: "/"))
private static func getSizeOfFolders(startingAt:String) -> [Int] {
let dirs = getFiles(inFolder: startingAt)
var out:[Int] = []
for dir in dirs {
out.append(getSize(ofFolder: dir))
}
return out
}
private static func getSize(ofFolder:URL) -> Int {
var folderSize = 0
(try? FileManager.default.contentsOfDirectory(at: ofFolder, includingPropertiesForKeys: nil))?.lazy.forEach {
folderSize += (try? $0.resourceValues(forKeys: [.totalFileAllocatedSizeKey]))?.totalFileAllocatedSize ?? 0
}
print("\(ofFolder): \(folderSize)")
return folderSize
}
private static func getFiles(inFolder:String) -> [URL] {
let fileURL = URL(fileURLWithPath:inFolder)
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: fileURL, includingPropertiesForKeys: nil, options: [])
return directoryContents
} catch {
print(error.localizedDescription)
return []
}
}
Output
file:///.HFS+%20Private%20Directory%20Data%0D/: 0
file:///.file: 0
file:///.mb/: 0
file:///Applications/: 0
file:///Developer/: 16384
file:///Library/: 0
file:///System/: 0
file:///bin/: 0
file:///cores/: 0
file:///dev/: 0
file:///etc: 0
file:///private/: 0
file:///sbin/: 0
file:///tmp: 0
file:///usr/: 0
file:///var: 0
[0, 0, 0, 0, 16384, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]