Skip to content

Instantly share code, notes, and snippets.

@jayz5
Last active September 24, 2018 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayz5/1b0da03a06ae14a8d2983387fc5adbe6 to your computer and use it in GitHub Desktop.
Save jayz5/1b0da03a06ae14a8d2983387fc5adbe6 to your computer and use it in GitHub Desktop.
extension FileManager {
func checkFileSizes(in folderURL: URL) {
do {
let resourceKeys: [URLResourceKey] =
[.fileSizeKey, .fileAllocatedSizeKey, .isRegularFileKey, .isDirectoryKey]
for url in try contentsOfDirectory(at: folderURL,
includingPropertiesForKeys: resourceKeys,
options: [])
{
let resourceValues = try url.resourceValues(forKeys: Set(resourceKeys))
print("\n\(url.lastPathComponent) resources: \n")
print(" \(URLResourceKey.fileSizeKey.rawValue): \(resourceValues.fileSize ?? 0)")
print(" \(URLResourceKey.fileAllocatedSizeKey.rawValue): \(resourceValues.fileAllocatedSize ?? 0)")
if let isDirectory = resourceValues.isDirectory, isDirectory {
checkFileSizes(in: url)
}
}
} catch {
print("check file size errors: \(error)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment