Create folder structure in Gdrive

It is possible to create a single folder or hierarchical folders in Gdrive to save images under the app folder (see Workflow to create a new folder in drive)

However, it may not be sufficient for your needs as you need a different file structure for organizing miscellaneous files of different file types.

Here is a bit of google apps script code that may be helpful to create a folder structure.

var GDRIVEFOLDERID = "1234567890abcd"

function doPost(e) {
  
  var json = e.postData.contents;
  var body = JSON.parse(json)
  
  Logger.log( "Entering doPost - received " + JSON.stringify(body) );
 
  try {
    createHierFoldersinGDrive(body.foldername);
    return ContentService    // return json success results
          .createTextOutput(
            JSON.stringify({"result":"success",
                            "data": JSON.stringify(e.parameters) }))
          .setMimeType(ContentService.MimeType.JSON);
  }
  catch(e) {
      return ContentService    // return json failure results
          .createTextOutput(
            JSON.stringify({"result":"failure",
                            "data": "Missing parameter" }))
          .setMimeType(ContentService.MimeType.JSON);
  }	  
}  

function createHierFoldersinGDrive(folder) {

  var folders = ["SubFolder1", "SubFolder2", "SubFolder3", "SubFolder4"]

  //Create the folder for the new property inside the specified folder of G Drive
  var newFolder = DriveApp.getFolderById(GDRIVEFOLDERID).createFolder(property);

  for each (var folder in folders) {
    newFolder.createFolder(folder);
  }
  
  Logger.log("Folders created successfully in G Drive");

}
6 Likes