Search for folders and display it’s child folders in Google Drive with Apps Script

Walden Systems Geeks Corner News Search for folders and display it’s child folders in Google Drive with Apps Script Rutherford NJ New Jersey NYC New York City North Bergen County

Working with Google Drive one quickly realizes there are many good reasons to automate many tasks. Simple tasks, like creating, gathering and displaying folders is more than tedious and time consuming. In this blog we will go over a useful function and class to search for a folder by name and display it’s child folders. This functionality is built on top of previous blogs such as, ’Create a Folder in the Root Drive’ and ’Iterate and Display folder’s full path’.

I often work with data sets that are populated from a database or another spreadsheet to analyze big chunks of data. This requires creation of folders and files on demand that will hold my spreadsheets and data. The folders and files are created and manipulated dynamically, therefore the data used needs to be cleaned up and removed from my Google Drive when the process has completed. Once I have my spreadsheet built out from my datasets, I automate many of the tasks using my Google Apps Script code base. In this blog we will utilize the DriveApp Class to manage, create and find files and folders in Google Drive. Since we are working primarily in folder context, we will need to manipulate folders using Folder Class, which creates, deletes and manages folders.


To start, we must first search for our parent folder. The DriveApp Class function allows us to narrow down a specific set of folders using a name search: 'DriveApp.getFoldersByName("folder-string-name")'. This function returns a 'FolderIterator' object that we will use to gather the child folders for each parent folder.

We will start with a simple function that will return an array of children folders. The returned folders will have the following string format, '/parent-folder/child-folder'

Our function signature takes just one parameter: 'parentFolder'.


function getChildFolderUrls(parentFolder) 
We then call ‘parentFolder.getFolders()’ which returns a FolderIterator Class which gathers all the child folders. This object allows scripts to iterate over a large collection of folders. Now that we have control of our child folders, we need to find the parent of each folder. To get the parent folder we call the function ‘folder.getParents()’. This allows us to construct our full path URL by calling a helper function:

function getFolderParentPath(folderIter) 
Putting it all together:

function createNewFolder(folderName) {
  var folder = DriveApp.createFolder(folderName);
  return(folder);
};

function getFolderParentPath(foldersIter) {
  var folderUrl = "/";
  while (foldersIter.hasNext()){
    var folder = foldersIter.next();
    folderUrl = folderUrl + folder.getName() + "/";
  }
  return(folderUrl);
};

function getChildFolderUrls(parentFolder) {
  var parentUrlStr = "";
  var parentUrlArr = [];

  //get children folders
  var foldersIter = parentFolder.getFolders();

  while (foldersIter.hasNext()) {
    var nextFolder = foldersIter.next();
    parentUrlStr = getFolderParentPath(nextFolder.getParents());
    parentUrlStr = parentUrlStr + nextFolder.getName();
    parentUrlArr.push(parentUrlStr);
  };
  return(parentUrlArr);
};

function myMainBlog() {
  var parentFolder = null;
  var parentFolderUrls = [];
  var childFolderUrls = [];
  var allFolderUrls = [];
  var folderNameIter = DriveApp.getFoldersByName("ebay");

  if(folderNameIter.hasNext()){
    while( folderNameIter.hasNext() ) {
      parentFolder = folderNameIter.next();
      parentFolderUrls.push("/" + parentFolder.getName());
    };
  };
  childFolderUrls = getChildFolderUrls(parentFolder)
  allFolderUrls = parentFolderUrls.concat(childFolderUrls);
  Logger.log(allFolderUrls);
};