Forum Discussion

James_T's avatar
James_T
Helpful | Level 6
4 years ago

Quickest way to check if a folder and subfolders are empty?

I only want the Dropbox Embedder to show if the specified folder actually have any files in it.

What is the best way to figure out if a folder (or subfolder) has any files?

At first I thought about "list_folder" and loop though the result to see if theres any file. But list_folder only returns 1 directory, and apparently need to use "continue" to get anything more? That's a bunch of queries!

The "Search" endpoint was my next thought, but I'm not sure how to search only for files?

Anyone have any suggestions?

3 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 years ago

    While it may require more than one API call, using the /2/files/list_folder[/continue] endpoints (or corresponding native methods in the SDKs) is the best way to check if a particular folder is empty or not. The exact number of entries per page and total number of pages is not guaranteed and may vary by folder/account, so you would need to call back to /continue until 'has_more' is returned as false. I'll also pass this along as a feature request for a simpler way to check that, but I can't promise if or when that might be implemented.

  • James_T's avatar
    James_T
    Helpful | Level 6
    4 years ago

    Thanks for your reply.

    The folder I tested list_folder with, contains only 4 directories. The API call only returned 1 folder (+ the root folder). Is that normal behavior?

    It thought it would only require the continue call if there were lots of folder/files?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    4 years ago

    The number of entries you get back per page is not guaranteed, and it may require multiples calls even if there is not a large number of entries. You'll need to implement both list_folder and list_folder/continue and always call back for more entries until has_more is false in order to get the full listing.

About Dropbox API Support & Feedback

Node avatar for 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!