cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox 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: 

search dropbox folder

search dropbox folder

bbongrip
Helpful | Level 6
Go to solution

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.  

1 Accepted Solution

Accepted Solutions

bbongrip
Helpful | Level 6
Go to solution
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

View solution in original post

14 Replies 14

Greg-DB
Dropbox Staff
Go to solution
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.

bbongrip
Helpful | Level 6
Go to solution
thanks 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-DB
Dropbox Staff
Go to solution

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.

bbongrip
Helpful | Level 6
Go to solution

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 Staff
Go to solution

I don't see in your code where you're attempting the search call. Can you share that part? Thanks!

bbongrip
Helpful | Level 6
Go to solution
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 }

bbongrip
Helpful | Level 6
Go to solution
Im thinking ListFolderResults is more what I'm looking for?

Greg-DB
Dropbox Staff
Go to solution

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.

bbongrip
Helpful | Level 6
Go to solution

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!

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    bbongrip Helpful | Level 6
What do Dropbox user levels mean?