Last active
June 8, 2024 11:46
-
-
Save benigumocom/6a8e4506438b5260469c12b1c12c0fb7 to your computer and use it in GitHub Desktop.
【Swift】ファイルやディレクトリ操作するための extension をまずは作った 👉 https://android.benigumo.com/20240608/file-ex/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension URL { | |
var exists: Bool { | |
isFile || isDirectory | |
} | |
var isFile: Bool { | |
(try? resourceValues(forKeys: [.isRegularFileKey]))?.isRegularFile == true | |
} | |
var isDirectory: Bool { | |
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true | |
} | |
var directoryEntryCount: Int { | |
(try? resourceValues(forKeys: [.directoryEntryCountKey]).directoryEntryCount) ?? 0 | |
} | |
var fileSize: String { | |
let size = (try? resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0 | |
return size.formatted(.byteCount(style: .file)) | |
} | |
var modified: Date { | |
try! resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate! | |
} | |
func shortPath(percentEncoded: Bool = true) -> String { | |
path(percentEncoded: percentEncoded) | |
.replacingOccurrences( | |
of: URL.homeDirectory.path(percentEncoded: percentEncoded), | |
with: "/HOME/" | |
) | |
} | |
} | |
extension FileManager { | |
private func contents(directory url: URL) -> [URL] { | |
(try? contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [])) ?? [] | |
} | |
func showContents(_ url: URL = .documentsDirectory) { | |
print( | |
String(format: "%@ %@", url.shortPath(), url.isFile ? "[\(url.fileSize)]" : "") | |
) | |
if url.isDirectory { | |
contents(directory: url) | |
.sorted(by: { | |
let lr = [$0, $1].map { | |
($0.path().components(separatedBy: "/").dropLast().joined(), $0.path()) | |
} | |
return lr[0] < lr[1] | |
}) | |
.forEach { content in | |
showContents(content) | |
} | |
} | |
} | |
} | |
// ex. | |
let manager = FileManager.default | |
let documents = URL.documentsDirectory | |
print(documents.shortPath()) | |
// /HOME/Documents/ | |
manager.showContents(documents) | |
// /HOME/Documents/ | |
// /HOME/Documents/another/ | |
// /HOME/Documents/another/copied.txt [6 bytes] | |
// /HOME/Documents/some/ | |
// /HOME/Documents/some/copied.txt [6 bytes] | |
// /HOME/Documents/some/moved.txt [6 bytes] | |
print() | |
print(documents.exists) | |
// true | |
print(documents.isDirectory) | |
// true | |
print(documents.isFile) | |
// false | |
print(documents.isFileURL) // built-in | |
// true | |
print(documents.directoryEntryCount) | |
// 2 | |
print(documents.fileSize) | |
// Zero kB | |
print(documents.modified) | |
// 2024-06-08 11:33:07 +0000 | |
print(documents.shortPath(percentEncoded: true)) | |
// /HOME/Documents/ | |
let dummy = documents.appending(component: "dummy") | |
print(dummy.shortPath()) | |
// /HOME/Documents/dummy | |
manager.showContents(dummy) | |
// /HOME/Documents/dummy | |
print(dummy.exists) | |
// false | |
print(dummy.isDirectory) | |
// false | |
print(dummy.isFile) | |
// false | |
print(documents.isFileURL) // built-in | |
// true | |
print(dummy.directoryEntryCount) | |
// 0 | |
print(dummy.fileSize) | |
// Zero kB | |
//print(dummy.modified) | |
// crash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment