Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check 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 dr...
- 9 years agoI 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
bbongrip
9 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-DB
Dropbox Community Moderator
9 years agoI 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!
About 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!