Skip to main content
Question

API: get list of files in the folder recursively

  • May 22, 2025
  • 6 replies
  • 114 views

Forum|alt.badge.img

Is there an API call I can use to get list of all files and folders in a specific folder? I see there is a "search" call but it requires a "query" parameter and does not accept "*".

6 replies

Forum|alt.badge.img

Hi ,

 

You'll want the List items in folder endpoint, that should give you the information that you're looking for.

 

- Jon


Forum|alt.badge.img

It shows files/folders in *current* directory only. I need a recursive list.


Forum|alt.badge.img

The code below works for me. Here's my folder/file structure:

  • Folder1
  • * file1.1
  • * file1.2
  • Folder2
  • * file2.1
  • * file2.2
  • file1
  • file2

BOX_oULg_PGzwSAolM_0xP57-w.png

BOX_Tod9-6n_65NgnOA4nP40AQ.png

BOX_5uxjHTuCbJyy18bI5c1weQ.png

import os
import boxsdk

auth = boxsdk.OAuth2(
client_id=config['boxAppSettings']['clientID'],
client_secret='',
access_token=access_token
)

client = boxsdk.Client(auth)

def get_recursive(dir_list):
for item in dir_list[-1].get_items():
file_info = item.get()
if file_info.type == "folder":
dir_list.append(item)
yield from get_recursive(dir_list)
else:
yield "/".join([x.get().name for x in dir_list]) + "/" + item.get().name
dir_list.pop()

print(os.linesep.join(list(get_recursive([client.folder('0')]))))

Which for my structure prints:

All Files/Folder1/file1.1
All Files/Folder1/file1.2
All Files/Folder2/file2.1
All Files/Folder2/file2.2
All Files/file1
All Files/file2


Forum|alt.badge.img

Hi I am new to box apis. Just one question to @Jason Friedman  does it work for very large numbers of folders?


Forum|alt.badge.img

Yes ... but, the token times out after 60 minutes. You will need to add to my code try/except blocks to identify that situation and get a new token.


Forum|alt.badge.img

Indeed that is probably true. I no longer have my test setup but I believe something like this would likely fix the bug you have identified:

import os
import boxsdk

auth = boxsdk.OAuth2(
client_id=config['boxAppSettings']['clientID'],
client_secret='',
access_token=access_token
)

client = boxsdk.Client(auth)

def get_recursive(dir_list):
for item in dir_list[-1].get_items():
file_info = item.get()
yield "/".join([x.get().name for x in dir_list]) + "/" + item.get().name
if file_info.type == "folder":
dir_list.append(item)
yield from get_recursive(dir_list)
dir_list.pop()

print(os.linesep.join(list(get_recursive([client.folder('0')]))))