@DCuser122
The folder.get_items
returns few details and several object types, like file
, folder
, and weblink
So we have to do this in 3 steps:
- From the items grab only files
- For each item representing a file, grab the details, which includes the modified at
- Sort that list
Consider this method:
def files_in_folder(client: Client, folder_id: str = "0"):
try:
folder = client.folder(folder_id=folder_id).get()
except BoxAPIException as e:
if e.status == 404:
print(f"Folder {folder_id} not found\n")
return
print(f"Items in: {folder.name} {folder.id}")
print("-" * 80)
items = folder.get_items()
files = [item.get() for item in items if item.type == "file"]
files.sort(key=lambda x: x.modified_at, reverse=True)
for file in files:
print(f"{file.type} {file.modified_at} ({file.id}) {file.name} ")
newest_file = files[0]
print(f"\nNewest file:{file.modified_at} {newest_file.name} {newest_file.id}")
Modifying the main method:
def main():
client = get_box_client(None)
user = client.user(USER_ID).get()
as_user_client = client.as_user(user)
me = as_user_client.user().get()
print(f"As-User account: {me.name} {me.id}")
files_in_folder(as_user_client, "165803865043")
Results in:
As-User account: Rui Barbosa 18622116055
Items in: Preview Samples 165803865043
--------------------------------------------------------------------------------
file 2023-05-17T06:50:27-07:00 (1216393081021) BoxAPISingDemoDoc (1) (2).pdf
file 2023-03-06T11:30:16-08:00 (974225001875) ZIP_renamed.zip
file 2023-03-06T11:23:12-08:00 (974207525964) Audio_renamed.mp3
file 2022-08-17T07:28:54-07:00 (997819641379) mov_bbb.mp4
file 2022-08-16T14:53:38-07:00 (997509657533) BoxAPISingDemoDoc (1) (1).pdf
file 2022-06-21T14:01:14-07:00 (974226696777) BoxAPISingDemoDoc (1).pdf
file 2022-06-21T13:55:01-07:00 (974229112148) Single Page.docx
file 2022-06-21T13:55:00-07:00 (974225083627) JSON.json
file 2022-06-21T13:55:00-07:00 (974225084827) Preview SDK Sample Excel.xlsx
file 2022-06-21T13:54:59-07:00 (974228631587) Document (Powerpoint).pptx
file 2022-06-21T13:54:59-07:00 (974209854641) HTML.html
file 2022-06-21T13:54:59-07:00 (974227441126) JS-Small.js
Newest file: BoxAPISingDemoDoc (1) (2).pdf 1216393081021
Not sure if time zones are important in this situation, but consider them.
Let us know if this works for you.
Cheers