How to find folder ID by path in Google Drive API?

Viewed 2141

I'm able to find a Google Drive Folder's ID by searching its name, but how can I find the Folder ID by searching its path?

I'm searching within a Drive that has multiple folders with the same name, but different paths, so I want to ensure I'm finding the correct folder ID.

results = service.files().list(q="name='folder_name'",
        pageSize=10, fields="nextPageToken, files(id, name)",
        supportsAllDrives=True, driveId='driveId', includeItemsFromAllDrives=True, corpora='drive').execute()

The above returns multiple folders. I don't see a query parameter for the path.

Sample folder structure:

Food
   \Vegetables
      \Folder 1
        Item 1
        Item 2
        Item 3
      \Folder 2
        Item 1
   \Fruit
      \Folder 1
   \Protein
1 Answers

How about this answer?

Issue:

Unfortunately, in the current stage, there are no methods for directly retrieving the files and folders in the specific folder using the folder name. So in your case, it is required to use a workaround.

Workaround:

I would like to propose the workaround as follows.

From your sample folder structure, I could understand that you want to retrieve the file list under the folders of Folder 1 and Folder 2 in Vegetables. In this case, how about using the following search query?

q="name='Folder 1' and '### folderId of Vegetables ###' in parents"

In this case, please retrieve the folder ID of Vegetables. By above modified search query, the folder ID of Folder 1 in the folder of Vegetables can be retrieved.

Of course, you can also retrieve the folder ID of Vegetables using the script.

Note:

  • In this answer, it supposes that you have already been able to get the file metadata using Drive API.

References:

Related