Filemanager working with Volumes ?

Can't find info on working with Volumes. Need to know how to work outside the Users folder / get the root folder / check if files or directories are on same volume / copy or move from one to another etc?

Replies

What's more precisely the problem ?


There are 2 functions to find the volumes.

func mountedVolumeURLs(includingResourceValuesForKeys: [URLResourceKey]?, options: FileManager.VolumeEnumerationOptions) -> [URL]?

Returns an array of URLs that identify the mounted volumes available on the device.


func subpathsOfDirectory(atPath: String) -> [String]


Once you have their URL, you can search for their directory… But may be I misunderstood your question.

Need to know how to … get the root folder

For a file system URL, you can get the root of the volume using the

.volumeURLKey
. For example:
let u = URL(fileURLWithPath: "/Volumes/SDKCentral/MacOSX10.14.sdk/", isDirectory: true)
let info = try u.resourceValues(forKeys: [.volumeURLKey])
print(info.volume!) // prints: file:///Volumes/SDKCentral/

check if files or directories are on same volume

The canonically correct way to do this is with

.volumeIdentifierKey
.

copy

There’s nothing special here;

FileManager
will just as easily copy between volumes as it does within a volume.

move from one to another etc?

FileManager
will also move between volumes. Be aware that this is not supported directly by the underlying file system. To accomplish this,
FileManager
does a copy then delete.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for the responses, has been helpful. 🙂