I'm trying to display data on available disks. One of the things I want to display is its File System Type. What should I add to this piece of code:\
extension FileManager {
static var mountedVolumes: [URL] {
(FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil) ?? []).filter({$0.path.hasPrefix("/Volumes/")})
}
}
extension URL {
var volumeTotalCapacity: Int? {
(try? resourceValues(forKeys: [.volumeTotalCapacityKey]))?.volumeTotalCapacity
}
var volumeAvailableCapacityForImportantUsage: Int64? {
(try? resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey]))?.volumeAvailableCapacityForImportantUsage
}
var name: String? {
(try? resourceValues(forKeys: [.nameKey]))?.name
}
}
for url in FileManager.mountedVolumes {
print("")
print(url.name ?? "Untitled")
print("Capacity:", url.volumeTotalCapacity ?? "nil")
print("Available:", url.volumeAvailableCapacityForImportantUsage ?? "nil")
print("File System type:", url.isFileURL)
//print("File System Type:", )
}
CodePudding user response:
You should use the URLResourceKey volumeLocalizedFormatDescriptionKey
to get the disk file system
let fileManager = FileManager.default
let resourceKeys: [URLResourceKey] = [.volumeNameKey, .volumeLocalizedFormatDescriptionKey]
if let volumes = fileManager.mountedVolumeURLs(includingResourceValuesForKeys: resourceKeys, options: .skipHiddenVolumes) {
for volume in volumes {
if let resources = try? volume.resourceValues(forKeys: Set(resourceKeys)),
let name = resources.volumeName,
let format = resources.volumeLocalizedFormatDescription {
print("\(name), format: \(format)")
}
}
}
Example from my machine with an external drive and a SD card attached
Macintosh HD, format: APFS
Bilder-Arkiv, format: Mac OS Extended (Journaled)
LEICA M9, format: MS-DOS (FAT32)