You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

Shades's avatar
Shades
Explorer | Level 3
8 years ago

Get only files or only folders in a directory with Swiftydropbox

How would I go about to get a list with only files or only folders in a directory. As far as I know, I can get the metadata with the client.files.getMetaData, but that seems like a backwards way of doing it by first getting all files and then getting all metadata. Especially if I have basically two closures in each other to set for example an array to use as a datasource for a tableview.

  • Shades's avatar
    Shades
    Explorer | Level 3

    For example, this won't work, since the completion will be called too early:

     

     

    func getSubDirectory(path: String, completion: @escaping ([String]) -> ()) {
            var directories: [String] = []
            if let client = DropboxClientsManager.authorizedClient {
                client.files.listFolder(path: path).response { response, error in
                    if let result = response {
                        print("Folder contents:")
                        for entry in result.entries {
                            client.files.getMetadata(path: (entry.pathLower)!).response { response, error in
                                if let metadata = response {
                                    if let folder = metadata as? Files.FolderMetadata {
                                        print(folder.name)
                                        directories.append(folder.name)
                                    }
                                }
                            }
                        }
                        completion(directories)
                    } else {
                        print(error!)
                    }
                }
            }
        }

     

    • Greg-DB's avatar
      Greg-DB
      Icon for Dropbox Staff rankDropbox Staff

      You don't need to call getMetadata for each entry. You can perform the as? Files.FolderMetadata or Files.FileMetadata check on each Metadata object in the entries list returned by listFolder itself.

      • Shades's avatar
        Shades
        Explorer | Level 3

        I figured it out in the end, my solution ended up being like this:

         

        func getFolders(path: String, completion: @escaping ([String]) -> ()) {
                var directories: [String] = []
                if let client = DropboxClientsManager.authorizedClient {
                    client.files.listFolder(path: path).response { response, error in
                        if let result = response {
                            for entry in result.entries {
                                if let folder = entry as? Files.FolderMetadata {
                                    directories.append(folder.name)
                                }
                            }
                            directories.sort { $0.capitalized < $1.capitalized }
                            completion(directories)
                        } else {
                            print(error!)
                        }
                    }
                }
            }

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,879 PostsLatest Activity: 5 hours ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!