ND003
ND003

Reputation: 802

Check Existence of a folder using Dropbox SDK

In Dropbox SDK 2.0, is there a way to check the existence of a folder? Or do we use the brute force method of listing the folders and then scanning the list?

Upvotes: 5

Views: 4440

Answers (2)

ND003
ND003

Reputation: 802

Here is the complete Java code to check for existence of a folder and create if folder does not exist

DbxClientV2 dbxClient;
        try
            {           
                dbxClient.files().getMetadata("/MyFolder");
            }
        catch (GetMetadataErrorException e)
            {
                // TODO Auto-generated catch block
                if (e.errorValue.isPath())
                {
                    LookupError le = e.errorValue.getPathValue();
                    if (le.isNotFound())
                    {
                        System.out.println("Path doesn't exist on Dropbox: ");
                        try
                        {
                            dbxClient.files().createFolder("/MyFolder");
                        }
                        catch (CreateFolderErrorException e1)
                        {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        catch (DbxException e1)
                        {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                }
            }

Upvotes: 2

Greg
Greg

Reputation: 16930

You can use the /2/files/get_metadata endpoint to check for an existing folder at a given path. It will either return the metadata if it exists, or a path.not_found error if not.

You didn't mention which SDK you're referring to, but for example, in the Dropbox API v2 Java SDK, that corresponds to the DbxUserFilesRequests.getMetadata method.

Upvotes: 4

Related Questions