Cut the Clutter: Test Ignore Files Feature - sign up to become a beta tester here.
Forum Discussion
bbongrip
9 years agoHelpful | Level 6
search dropbox folder
can anybody tell me a way to search a specific folder in dropbox to populate an array of strings with the filenames in that folder? Im trying to implement a way for users to search for a file in a dropbox folder.
- I finally got it! lol Thank you so much for you time and help. It has been greatly appreciated and a learning experience for me. I ended up using "//" as my path and it works perfect.
Cheers!
Kevin
14 Replies
- Greg-DB9 years ago
Dropbox Community Moderator
Using the Dropbox API, you can search for files and folders under a particular path using the /2/files/search endpoint:
https://www.dropbox.com/developers/documentation/http/documentation#files-search
If you're using an official SDK, there will be a corresponding search method for that. - bbongrip9 years agoHelpful | Level 6thanks again greg. however I'm not really understanding how i would be able to search for all the filenames within a specific dropbox folder, and add those filenames to an array of strings, to then use as a base for my user to search for a specific string in that array. sorry if I'm not making to much sense. this is all very new to me. FYI - I'm using swift 3
- Greg-DB9 years ago
Dropbox Community Moderator
Since you're using Swift, are you using the official SwiftyDropbox SDK? In that case, you'd use the search method in FilesRoutes. As long as the call succeeds, it will return a SearchResult. You can then use the SearchResult.matches, which is an array of SearchMatch. SearchMatch.metadata contains the FileMetadata for the match, which contains the name, pathDisplay, etc.
Give that a try and let us know if you run in to any issues.
- bbongrip9 years agoHelpful | Level 6
I was trying the search func that returns a SearchResult but I couldnt get it working. here is some of my code.
hope this helps. I really appreciate your help
// MARK: - UISearchBarDelegate func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
var data = [String]()
// IN HERE I WANT TO SEARCH MY APPS FOLDER IN DROPBOX AND POPULATE THE "data" STRING ARRAY WITH THE FILE NAMES OF ALL THE FILES IN THE FOLDER
filtered = data.filter({ (text) -> Bool in let tmp: NSString = text as NSString let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive) return range.location != NSNotFound }) if(filtered.count == 0){ searchActive = false } else { searchActive = true } searchTableView.reloadData() // These next two lines dynamicaly adjust the Search TableView's height super.updateViewConstraints() self.searchTableViewHeightConstraint?.constant = self.searchTableView.contentSize.height } - Greg-DB9 years ago
Dropbox Community Moderator
I don't see in your code where you're attempting the search call. Can you share that part? Thanks!
- bbongrip9 years agoHelpful | Level 6
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { var data = [String]()
data = search(path: "/stuckbykev/", query: searchText, start: 0, maxResults: 100, mode: .filename) filtered = data.filter({ (text) -> Bool in let tmp: NSString = text as NSString let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive) return range.location != NSNotFound }) if(filtered.count == 0){ searchActive = false } else { searchActive = true } searchTableView.reloadData() // These next two lines dynamicaly adjust the Search TableView's height super.updateViewConstraints() self.searchTableViewHeightConstraint?.constant = self.searchTableView.contentSize.height } @discardableResult open func search(path: String, query: String, start: UInt64 = 0, maxResults: UInt64 = 100, mode: Files.SearchMode = .filename) -> RpcRequest<Files.SearchResultSerializer, Files.SearchErrorSerializer> { return RpcRequest } - bbongrip9 years agoHelpful | Level 6Im thinking ListFolderResults is more what I'm looking for?
- Greg-DB9 years ago
Dropbox Community Moderator
Thanks! Is that the exact code you're trying though? I don't see the Dropbox client object itself. You just seem to be calling a separate search method. Also, note that you need to implement the response callback. Just taking the return value won't give you what you're looking for.
The search method is an RPC-style call, so it would look like this:
https://github.com/dropbox/SwiftyDropbox#rpc-style-request
The Xcode autocomplete should also help with the parameters and response callback.
If you want to search for a particular string, that is the right method. Alternatively, if you want to just list everything in a folder, listFolder and listFolderContinue would be better.
- bbongrip9 years agoHelpful | Level 6
ok here is my code now. ive been trying a few different things. but the RPC request was throwing me off.
// MARK: - UISearchBarDelegate var data = Array<Files.Metadata>() var namesList = [String]() var filtered = [String]() func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { // Verify user is logged into Dropbox if let dropboxClient = DropboxClientsManager.authorizedClient { // List folder contents and append to string array for return dropboxClient.files.listFolder(path: "stuckbykev/").response { response, error in if let response = response { print(response) self.data = response.entries for file in self.data { self.namesList.append(file.name) } } else if let error = error { print(error) } } } print(namesList) filtered = namesList.filter({ (text) -> Bool in let tmp: NSString = text as NSString let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive) return range.location != NSNotFound }) if(filtered.count == 0){ searchActive = false } else { searchActive = true } searchTableView.reloadData() // These next two lines dynamicaly adjust the Search TableView's height super.updateViewConstraints() self.searchTableViewHeightConstraint?.constant = self.searchTableView.contentSize.height }
This is the error showing in the console, I think its a problem with my path string:
precondition failed: "stuckbykev/ must match pattern "\A(?:(/(.|[\r\n])*)?|(ns:[0-9]+(/.*)?))\z": file /Users/Kevin/Desktop/SwiftyDropbox-master/Source/SwiftyDropbox/PlatformNeutral/StoneValidators.swift, line 9
thanks again for all your help Greg!
- Greg-DB9 years ago
Dropbox Community Moderator
Yes, non-root paths in Dropbox API v2 should start with a "/", so you probably mean to use "/stuckbykev".
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,037 PostsLatest Activity: 21 hours ago
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 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!