Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

Shades's avatar
Shades
Explorer | Level 3
9 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.

3 Replies

  • Shades's avatar
    Shades
    Explorer | Level 3
    9 years ago

    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 Community Moderator rankDropbox Community Moderator
    9 years ago

    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
    9 years ago

    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.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

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, Facebook or Instagram.

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!