rryanp
rryanp

Reputation: 1027

How to get ID of Google Drive folder created with Google Script

I am using the following code to create a subfolder in a parent folder in Google Drive:

var parentfolder = DriveApp.getFolderById(parent_id);
var newFolder = DriveApp.createFolder('Child Folder');
parentfolder.addFolder(newFolder);

I want to now get the ID of the newly created folder (Child Folder). But the variable newFolder returns the folder name. I've also tried setting a variable equal to parentfolder.addFolder(newFolder), but that returns the name of the parentfolder. I know there's a getFoldersByName function I may be able to use to get to ID, but that seems risky as we have a lot of folders with the same name out there (we follow a consistent subfolder naming convention). The docs say it "gets a collection of all folders that are children of the current folder and have the given name," but how does it know what is the "current folder"?

Upvotes: 0

Views: 970

Answers (1)

Peter
Peter

Reputation: 5601

Use Folder.getId() to get the Id of the newly created folder. Like this:

var newFolder = DriveApp.createFolder('Child Folder');
Logger.log('The id of "Child Folder" is: %s', newFolder.getId() ); 
// Outputs: The id of "Child Folder" is: 0B_BaDTPJ7a-2TnFJanAtRXRzUTB

Upvotes: 1

Related Questions