cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to filter a folder by File Format using SwiftyDropbox?

How to filter a folder by File Format using SwiftyDropbox?

Shahaf L.
New member | Level 1

Hi 🙂

I’m working on an app that deals with text files and I want to present a list of files in a folder, but only files with a specific format. For example, only .txt or .md files.

Is it possible? If it is, How can I do that?

Thanks 🙂

6 Replies 6

Greg-DB
Dropbox Staff

The API itself doesn't offer filtering like that, so you'll have to use listFolder and do the filtering client-side, based on the extension in the metadata.

Shahaf L.
New member | Level 1

Hi Gregory,

I’m currently using this to view all the files in the folder:

client.files.listFolder(path: "\(folderPath)").response { response, error in

                print("*** List folder ***")

                if let result = response {

                    print("Folder contents:")

                    for entry in result.entries {

                        print(entry.name)

                        self.files.append(entry)

                    }

                    (self.view as! UITableView).reloadData()

                    print("Data Reloaded")

                } else {

                    print(error!)

                }

            }

 

If I understood you correctly, I should use this:

client.files.listFolder(path: "\(folderPath)").response { response, error in

                print("*** List folder ***")

                if let result = response {

                    print("Folder contents:")

                    for entry in result.entries {

                        print(entry.name)

                        if entry.name.rangeOfString(“.txt") != nil {

                           self.files.append(entry)

                        }

                    }

                    (self.view as! UITableView).reloadData()

                    print("Data Reloaded")

                } else {

                    print(error!)

                }

            }

Is this what you meant?

Thanks for the quick reply 🙂

Greg-DB
Dropbox Staff

Yes, that's the basic idea. 

Note that you probably want a more strict version of that though, since your code would match ".txt" anywhere in the name, e.g., I think your code would have a false positive on something like "my.txts.html". 

Also, make sure to differentiate between files/folders/deleted entries.

 

Shahaf L.
New member | Level 1

Hi Gregory,

This is what I ended up using:

                    for entry in result.entries {

                        client.files.getMetadata(path: entry.pathLower).response { response, error in

                            if let metadata = response {

                                if let file = metadata as? Files.FileMetadata {

                                    print("This is a file with path: \(file.pathLower)")

                                    if file.name.hasSuffix(".txt") || file.name.hasSuffix(".md") || file.name.hasSuffix(".html") {

                                        self.files.append(entry)

                                        print("File \(file.name) Added to the Array")

                                    } else {

                                        print("Unsupported File Format")

                                    }

                                } else if let folder = metadata as? Files.FolderMetadata {

                                    print("This is a folder with path: \(folder.pathLower)")

                                    self.files.append(entry)

                                }

                            } else {

                                print(error!)

                            }

                        }

                    }

I got a new question, but I’ll create a new post for it.

Thanks for the help! 🙂

Nikita I.1
New member | Level 1

Hi Shahaf,

even though it's not connected to your main question, why won't you try Swift pattern matching for path extension check, e.g.

let ext = (file.name as NSString).pathExtension;

switch (ext) {

    case "txt", "md", "html":

        self.files.append(entry)

    default:

        continue;

}

instead of having to check a suffix for each of your extensions?

 

Regards, Nik

Shahaf L.
New member | Level 1

Hi Nik,

That’s a nice idea 🙂

I’ll give it a try.

Thanks 🙂

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Shahaf L. New member | Level 1
  • User avatar
    Nikita I.1 New member | Level 1
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?