Skip to content
Advertisement

Create nested folders with Google Drive API

I need to add a new file in a nested folder on Google Drive using Google Drive api in the form YYYY/MM/DD/HH/mm/file where the dates are dynamically created basing on the current date. For example:

JavaScript

I created the following script in Python, but it is creating one single folder (named “2021/06/16/11/30”) instead of the desired nested structure (multiple nested folders named “06”, “11” and “30”) inside the parent folder “2021”:

JavaScript

Is there a way to create such nested folder structure in one call instead of iterate on each folder creation and pass the new folder id as new parent id?

Advertisement

Answer

Modification points:

  • At Google Drive, each file and folder are managed by the unique ID instead of the filename and folder name. In this case, when the file of my_file.csv is uploaded to the nested folder of 2021/06/16/11/30/, it is required to retrieve the folder ID from the folder name. And, when the folder is not existing, it is required to create new folder.
  • In order to achieve this, unfortunately, the method of service.files().list() is used in a loop. Because it is required to check each folder ID.

When above points are reflected to your script, it becomes as follows.

Modified script:

JavaScript
  • In this modified script, when the path is path = '2021/06/16/11/30', when the folder is not existing in each folder, the folder is created. When the folder is existing in each folder, new folder is not created.

References:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement