How do I create a hierarchical folder?

Asked 1 years ago, Updated 1 years ago, 43 views

I wrote the code below to create the folder "Second Bottom 1" and "Second Bottom 2" under the folder "Second", but it is created in the same hierarchy as "Second" as shown in the attached image. Why?

var folder=DriveApp.createFolder("first");
var folder1 = folder.addFolder(folder.createFolder("second"));
var folder2 = folder1.addFolder(folder.createFolder("second bottom 1"));
var folder2 = folder1.addFolder(folder.createFolder("second bottom 2"));

Enter a description of the image here

google-apps-script

2022-09-30 15:46

1 Answers

If you take out the part where you are creating the folder itself...

var folder=DriveApp.createFolder("first");
folder.createFolder("Second")
folder.createFolder("2nd bottom 1")
folder.createFolder("2nd bottom")

All folders are created under folder (first).This is why they are created in the same hierarchy.

Also, addFolder() is used to make the folder of arguments belong to more than one folder, so it should not be necessary in this case.

I think the following code will be the desired behavior.

var folder1 = DriveApp.createFolder("first");
var folder2 = folder1.createFolder("second");
folder2.createFolder("2nd bottom 1"; 
folder2.createFolder("2nd bottom"); 


2022-09-30 15:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.