Multiple FOLDER maker

Making multiple copies of folders in Google Drive is a slow, boring task. You select the folder you want, make a copy, then have to rename it, then repeat this again and again. Here you’ll learn how to create multiple folders and name them individually, really fast using Apps Script.

If you’ve already read my post “Multiple FILE maker”, then this code is essentially the same, just with two main differences, we use the methods getFolderbyId() and we use createFolder(). So, if you have read it, you may want to skip through and just note the differences.

First, we have a sheet where we will enter the details. There are three parts.

In cell A3 we type the fixed part of the folder name want, i.e. The part that is the same in all the newly-created folders.

In column B, we type the variable parts of the folder name, i.e. The parts we want to be unique in each folder.

Finally, in cell C3 we paste the full URL of the folder we want to put the new folders into.

We then choose ‘2-Make multiple folders’ from the “Files & Folders” menu.

Here are the folders it made, all individually named:

Obviously, it works best when there are a lot of folders to make.


THE CODE

Let’s go through the code step-by-step:

1. //Get Folders sheet, get fixed name, get variable names
2. //Extract ID from folder URL, get root folder
3. //Make new folders in root folder with fixed and variable names
4. function makeFolders() {
5.   //Get data on Folders sheet
6.   const ss = SpreadsheetApp.getActiveSpreadsheet();
7.   const shFolders = ss.getSheetByName("Folders");
8.   const fixedName = shFolders.getRange(3, 1).getValues();

Line 4: First, we set up our function. Here I’ve called it makeFolders().

Line 6: Then, we get the current active spreadsheet, using getActiveSpreadsheet().

Line 7: Then, we get the sheet called “Folders”.

Line 8: Now, let’s get the fixed name. It’s in row 3, column 1 (I.e. A3), so in the getRange() method we add (3,1), then get its value using getValue().

9.  let variableNames = shFolders.getRange(1, 2)
10.                              .getDataRegion(SpreadsheetApp.Dimension.ROWS)
11.                              .getValues();
12.  variableNames.splice(0,2);

Line 9: Now, we need the variable names. We can use the method getDataRegion() to get a specific column of data. First, we get the top of that column (column B, cell B1).

Line 10: Then get the data region we want, which are the rows in that column.

Line 11: Then we get the values in those rows and these values are stored in the variable variableNames.

Line 12: We don’t want the first two rows, as these are blank and contain the column description. To do so, we can use splice() to remove the first two values of the array. 0 states the position, and 2 is the number of values to remove.

14. //Get folder URL
15.  const rootFolderUrl = shFolders.getRange(3, 3).getValue();

Line 15: Next, we get the URL of the folder we want to add the new folders into, which is in cell C3.

17.  //Check folder URL isn't blank
18.  if (rootFolderUrl !== "") {

Line 18: Let’s add a check to make sure the folder URL has been added. It will throw an error if it’s absent.

20.    //Extract root folder ID and get folder
21.    const rootFolderId = rootFolderUrl.match(/[-\w]{25,}/);
22.    const rootFolder = DriveApp.getFolderById(rootFolderId);

Line 21: Now, we need to extract the folder ID from the folder URL. We can easily do that by using the match method and passing the regular expression in the brackets. (more info here).

Line 22: Now, we use that ID stored in rootFolderId, to get the folder by its ID, using the DriveApp class and the getFolderById() method, which basically, gets the folder we want to add folders into.

24.    //Create a folder in the root for each of the variable names
25.    //and name each with the fixed and variable name 
26.    variableNames.forEach((variableName) => {
27.      rootFolder.createFolder(fixedName + variableName);
28.    })

Line 26: Now, we’re going to create folders in the root folder. First, we use forEach() to loop through the variable names stored in the variable variableNames. Each variable name will be in variableName, e.g. A, B, C.

Line 27: Around each loop we create a folder and give it a name which is a combination of the fixed name and current variable name, e.g. NEW FOLDER-A. Then we close the forEach loop.

29.    ss.toast("Your folders have been made! :) Baz", "Finished", 3);
30.  }
31. }

Line 29: Finally, just to tell the user the process has finished, I’ve added a toast message, which will pop up in the bottom right-hand corner, when the program has finished. Here it gets the active Spreadsheet ss, then in the brackets contains 3 parts: message, title, number of seconds it will be displayed.

Lines 30 and 31: This closes the if statement from line 18, and the function.


Adding a menu

1. // Add menu
2. function onOpen() {
3.   const ui = SpreadsheetApp.getUi();
4.   ui.createMenu('Files & Folders')
5.     .addItem('1-Make multiple files', 'makeFiles')
6.     .addSeparator()
7.     .addItem('2-Make multiple folders', 'makeFolders')
8.     .addToUi();
9. }

To run the program from a menu in the spreadsheet, I’ve also added a separate script file called onOpen, which will add a little menu to run the code.

Line 2: Call the function onOpen(), so that it opens automatically every time the spreadsheet is opened.

Line 3: We get the spreadsheet UI (user inferface).

Line 4: We create a menu in the UI and let’s call it “Files & Folders”.

Line 5: We add the first menu item to that menu. The first one is related to my post on making multiple files.

Line 6: As we have two menu items, let’s add a separator between them, so our user can click on the right option.

Line 7: The second menu item is called “2-Make multiple folder” and link it to the makeFolders() function.

Lines 8 and 9: Then we add it all to the UI and close the function.

Run the function and authorise the script. The newly-created files will appear in the same folder as the copied file.


Here’s the link to the Sheet and code.


Want to learn more about Google Workspace and Apps Script? The books below are available on Amazon. Just click on a book! (Affiliate links).

JavaScript Fundamentals for Apps Script users

a

Google Apps Script Projects 1
Google Apps Script Projects 2

11 comments

  1. Hello Baz, the link to the sheet is not for Folder maker but File maker. Could you change it?

  2. This is great! Is there a code modification that will allow each folder to be shared with someone different?

    1. Yes, you can use DriveApp.addEditor(emailAddress) to add others to a folder. As the code loops, you could add a column on the spreadsheet that lists the email addresses you want to share the folders with, and that it picks up an email as it loops through.

    1. Yes, you would just need to get the folder ID, which the other folders nest into and use that to then create the new folders. So, rather than getting the root folder ID (My Drive folder) you would use the folder ID of the folder you want to add folders into.

  3. Hello, this is so helpful! Is there a way to also make it return the link locations of the folders created?

    1. Hi-You could add this line after the createFolder line (L18): masterSheet.getRange(n+2,5).setValue(newFolder.getUrl()); This will add folder URLs in column 5 (you’ll have to make sure there are 5 columns in the sheet).

  4. Hi BAz,
    This is great! Thank you so much.
    I seem to be missing something, could you please help me understand where do I place the OnOpen() code to have this menu show up? Do I start this as a Text file? Do you have a copy of that, the code here seems like a screenshot so I can not copy it.

    1. Hi – You can either put the onOpen function before or after the main makeFolders function in the same Script Editor.
      When the file is opened, it will automatically see that there is an onOpen trigger and run it to open the menu.

Comments are closed.