Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
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.
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.
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 }
I don't see in your code where you're attempting the search call. Can you share that part? Thanks!
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 }
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.
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!
Hi there!
If you need more help you can view your support options (expected response time for a 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!