Iterate and Display folder’s full path in Google Drive with Apps Script

Walden Systems Geeks Corner News Iterate and Display folder’s full path 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 display folders from your root drive. This is built on top of the previous blogs to ’Create a Folder in the Root Drive’.

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. Once the folders and files are created and manipulated, the data used needs to be cleaned up and removed from my Google Drive. This requires various cleanup operations that remove the temporary files and folders. Once I have my spreadsheet built out from my datasets, I automate many of the tasks from within 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 manages folders under DriveApp Class.


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 that 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 getFolderParentPath(folderIter) {
  var folderUrl = "/";
  while (folderIter.hasNext()){
    var folder = folderIter.next();
    folderUrl = folderUrl + folder.getName() + "/";
  }
  return(folderUrl);
};

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

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

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

function myMainBlog() {
  var rootFolder = DriveApp.getRootFolder();
  Logger.log("/" + rootFolder.getName());
  var childFolderUrls = getChildFolderUrls(rootFolder);
  Logger.log(childFolderUrls);
};