Welcome to the new Box Support website. Check out all the details here on what’s changed.

API: get list of files in the folder recursively

New post

Comments

6 comments

  • jcleblanc

    Hi ,

     

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

     

    - Jon

    0
    Comment actions Permalink
  • downunder

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

    0
    Comment actions Permalink
  • Jason Friedman

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

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

    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

    1
    Comment actions Permalink
  • sushant prajapati

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

    0
    Comment actions Permalink
  • Jason Friedman

    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.

    0
    Comment actions Permalink
  • Jason Friedman

    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')]))))

    1
    Comment actions Permalink

Please sign in to leave a comment.