I'm trying to use the example code https://developer.apple.com/documentation/foundation/filemanager/2765464-enumerator to exclude a sub directory, but the .skipDescendants doesn't seem to do what I expect.
Should this work? I want to ignore everything in the __CYCLES folder inside topLevelFolder. But those paths are listed along with all the other files/folders. I never see "Skipping Cycles" in my output.
import Foundation
let rootPath = "/Volumes/volumename/topLevelFolder"
let topLevelFolderURL = URL(fileURLWithPath: rootPath)
let localFileManager = FileManager()
let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey, .fileSizeKey])
let directoryEnumerator = localFileManager.enumerator(at: topLevelFolderURL, includingPropertiesForKeys: Array(resourceKeys), options: [.skipsHiddenFiles, .skipsPackageDescendants])!
for case let fileURL as URL in directoryEnumerator {
guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
let isDirectory = resourceValues.isDirectory,
let name = resourceValues.name,
let size = resourceValues.fileSize
else {
continue
}
if isDirectory {
if name == "__CYCLES" {
directoryEnumerator.skipDescendants()
print("Skipping Cycles")
}
} else {
print("\(fileURL.path) = \(size)")
}
}
Post
Replies
Boosts
Views
Activity
I'm would like to make a copy of a folder with files and folders in it before I do anything to them. When I naively fileManager.copyItem() I get errors about .DS_Store files already existing (I think the Finder might be creating them before the copy gets to it). The destination directory is empty at the start of the process. Is there any way to tell copyItem to overwrite?
Is replaceItemAt() a way to do this, with backupItemName set to the original name and withoutDeletingBackupItem set in options? That seems so convoluted. Is there a better way?
I've had a little personal utility running for several versions of macOS that uses
let client = CWWiFiClient.shared()
if let ssid_name = client.interface()?.ssid()
to get the current SSID name and prints it (along with a bunch of other active network details.
With the most recent Sonoma Beta 2 and Xcode beta 2, this always returns nil.
Doing the same thing in a playground works as expected.
Is this a purposeful change or a bug I should file?