Get Subdirectories Using Swift

Get Subdirectories using Swift

There is no need to use a deep enumeration. Just get contentsOfDirectoryAtURL and filter the urls returned which are directories.

Xcode 11.4• Swift 5.2

extension URL {
func subDirectories() throws -> [URL] {
// @available(macOS 10.11, iOS 9.0, *)
guard hasDirectoryPath else { return [] }
return try FileManager.default.contentsOfDirectory(at: self, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]).filter(\.hasDirectoryPath)
}
}

usage:

 do {
let documentsDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = documentsDirectory.appendingPathComponent("pictures", isDirectory: true)
if !FileManager.default.fileExists(atPath: url.path) {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
}
let subDirs = try documentsDirectory.subDirectories()
print("sub directories", subDirs)
subDirs.forEach { print($0.lastPathComponent) }
} catch {
print(error)
}

Listing (only) the subfolders within a folder -- Swift 3.0 / iOS 10

You can pass URLResourceKey.isDirectoryKey to the includingPropertiesForKeys parameter in your contentsOfDirectory method call on line 6. This should return that particular attribute for all your returned URLs. Then, you can check the attribute to see if each URL is for a directory or a file ...

If you are wondering how to get at the attributes for the fetched URLs, then take a look at the resourceValues method for the URL class :)

(Swift) How to make subfolders in one folder

Your code should be like below,

extension FileManager.SearchPathDirectory {
func createSubFolder(named: String, withIntermediateDirectories: Bool = true) -> Bool {
guard let url = FileManager.default.urls(for: self, in: .userDomainMask).first else { return false }
do {
try FileManager.default.createDirectory(at: url.appendingPathComponent(named), withIntermediateDirectories: withIntermediateDirectories, attributes: nil)
return true
} catch {
print(error)
return false
}
}
}

I have just set withIntermediateDirectories's default value to true.

createIntermediates: If true, this method creates any nonexistent parent directories as part of creating the directory in url. If false, this method fails if any of the intermediate parent directories does not exist.

listing all files in a folder recursively with swift

FileManager has also a method for a deep search: enumerator(at:includingPropertiesForKeys:options:errorHandler:)

To get only the files you have to iterate the enumerator and filter the files

let url = URL(fileURLWithPath: "/path/to/directory")
var files = [URL]()
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
for case let fileURL as URL in enumerator {
do {
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey])
if fileAttributes.isRegularFile! {
files.append(fileURL)
}
} catch { print(error, fileURL) }
}
print(files)
}

It's highly recommended to use URLs rather than string paths.

Get all URLs for resources in sub-directory in Swift

Consider that the yellow folders Sample Image are virtual groups, not real folders (although Xcode creates real folders in the project directory). All files in the yellow folders are moved into the Resources directory in the bundle when the app is built.

Real folders in the bundle are Sample Image in the project navigator.

Get nil when looking for file in subdirectory of main bundle

The solution is to use

Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "games/game1")

I don't know why, but it doesn't work if we get a path, then construct an URL from it.

Note: You must

  1. Copy items if needed
  2. Create folder references (not "Create groups")
  3. Add to targets

How to list all folders and their subdirectories/files in iPhone SDK?

Take into account that your app runs in a sandbox and would not be able to get any folder/file outside of that sandbox.

ObjectiveC

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *s in fileList){
NSLog(@"%@", s);
}

Swift 4

guard let documentsDirectory =  try? FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else { return }
guard let fileEnumerator = FileManager.default.enumerator(at: documentsDirectory, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions()) else { return }
while let file = fileEnumerator.nextObject() {
print(file)
}


Related Topics



Leave a reply



Submit