Skip to main content

Is there API endpoint to count the number of files under a folder?



We can get the counts by iteratively getting the counts under subfolder. This is resource intensive and does not seem to be a good way to get the counts especially because there is a request rate limit imposed.

Hi @ujjwalkarjee ,



Interesting question, how ever I’m not sure if you want to count just files or any object (files, folder, and web links), and also if you want to count on the folder level or in the folder level and all sub folders.



Can you clarify?



Anyway digging a bit more into this, I can see that both the folder info and the list items endpoint do provide a total, but this would be for all object types.



Let us know.



Best regards


I need the count of files under a folder and all other subfolders within the folder. I don’t need permissions, collaborations or any other objects.


Hi @ujjwalkarjee



There is no API endpoint to give you this information directly.



You can, however, create a method to recursively get this information.



Be aware that this will use one request per folder, and it will only work if the folder has less than 1000 items between files, folders or web-links.



If you expect that folders have more than 1000 items, then you need to modify the logic to get the next page, until you reach the end. Might be more explicit if you use the get_items.



This example is in Python, but I’m confident you’ll get the idea:



from typing import List



from boxsdk import JWTAuth, Client



from boxsdk.object.folder import Folder



def get_client() -> Client:

config = JWTAuth.from_settings_file("config.json")

return Client(config)



def get_folder_by_id(client: Client, folder_id: str) -> Folder:

return client.folder(folder_id=folder_id).get()



def get_total_files_in_folder(client: Client, folder_id: str, depth: int = 0) -> int:

folder = get_folder_by_id(client, folder_id)



if not hasattr(folder, "item_collection"):

return 0

# total_itmes = folder.item_collection.get("total_count")

# print("Folder Items: " + str(total_itmes))

entries = folder.item_collection.get("entries")

total_files = 0

for entry in entries:

if entry.type == "file":

total_files += 1

if entry.type == "folder":

total_files += get_total_files_in_folder(client, entry.id, depth + 1)

print(" " * depth + "Folder: " + folder.name + " total : " + str(total_files))

return total_files





def main():

client = get_client()

total_files = get_total_files_in_folder(client, "0")

print("Total Files: " + str(total_files))



if __name__ == "__main__":

main()





This results in:



  Folder: 100k total : 0

Folder: aaaa total : 0

Folder: Cenotes total : 0

Folder: Eagle Ray Bay total : 0

Folder: Barbosa total : 1

Folder: 2023-08-17 total : 1

Folder: Ras Mohamed total : 1

Folder: Sharks Bay total : 0

Folder: Thistlegorm total : 0

Folder: Tropicana total : 0

Folder: Bookings total : 7

Folder: Test total : 0

Folder: test total : 0

Folder: Uploads total : 9

Folder: Box UI Elements Demo total : 12

Folder: Classification Service Demo total : 1

Folder: JWT Folder for UI Sample Apps total : 1

Folder: Box-Dive-Waiver.pdf 2023-08-16 08.49.44 total : 2

Folder: Box-Dive-Waiver.pdf 2023-08-16 09.08.09 total : 2

Folder: My Signed Documents total : 4

Folder: Shared with RB total : 1

Folder: Waivers total : 4

Folder: Webhook total : 0

Folder: All Files total : 30

Total Files: 30



Hope this helps.


Cheers


Thanks for all the details.


Reply