Skip to content

Instantly share code, notes, and snippets.

@toshi0383
Last active June 26, 2018 23:22
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 toshi0383/13de25b0b6ab55f33b6c80760b706867 to your computer and use it in GitHub Desktop.
Save toshi0383/13de25b0b6ab55f33b6c80760b706867 to your computer and use it in GitHub Desktop.
find file sizes in given directory
import Foundation
var fm: FileManager {
return .default
}
/// 指定されたディレクトリ以下のファイルサイズ合計を求める.
/// サブディレクトリ以下も再帰的に調べる.
/// 加算するメタデータ値はtotalFileAllocatedSize.
func findSize(at directoryPath: String) -> Int64? {
let properties: [URLResourceKey] = [.isRegularFileKey,
.totalFileAllocatedSizeKey,
/*.fileAllocatedSizeKey*/]
guard let enumerator = fm.enumerator(at: URL(fileURLWithPath: directoryPath),
includingPropertiesForKeys: properties,
options: .skipsHiddenFiles,
errorHandler: nil) else {
return nil
}
let urls: [URL] = enumerator
.flatMap { $0 as? URL }
.filter { $0.absoluteString.contains(".frag") }
let regularFileResources: [URLResourceValues] = urls
.flatMap { try? $0.resourceValues(forKeys: Set(properties)) }
.filter { $0.isRegularFile == true }
let sizes: [Int64] = regularFileResources
// mac上だとfileAllocatedSizeでもサイズ変わらなかった.
.flatMap { $0.totalFileAllocatedSize! /* ?? $0.fileAllocatedSize */ }
.flatMap { Int64($0) }
return sizes.reduce(0, +)
}
if let size = findSize(at: "/Users/a14786/Downloads/100.xcappdata/AppData/Library/com.apple.UserManagedAssets.Xm4IYH/C7DEE1A0-576A-479A-AE19-E009A91BEA25_373C6C03ABCA2CB2.movpkg") {
// print in GigaByte
print(size)
print(Double(size) / 1000 / 1000 / 1000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment