10
votes

I want to iterate through the tree structure of the folder in Google Drive using google apps script. The code below is listing some of the folders but not all. Can you advice what is best technique for drill down the folders structure? I am trying to log all folder names at first.

Thank you.

function test() {
 listSubfolders(DriveApp.getFolderById('FOLDER_ID'));
}

function listSubfolders(parentFolder) {
  var childFolders = parentFolder.getFolders();

 while(childFolders.hasNext()) {
   Logger.log(childFolders.next().getName());
   listSubfolders(childFolders.next());   

 }


}
4

4 Answers

11
votes

To access the sub folders, you can do in this way,

function getSubFolders(parent) {
  parent = parent.getId();
  var childFolder = DriveApp.getFolderById(parent).getFolders();
  while(childFolder.hasNext()) {
    var child = childFolder.next();
    Logger.log(child.getName());
    getSubFolders(child);
  }
  return;
}

function listFolders() {
  var parentFolder = DriveApp.getFolderById("0B1n6YLYwFmK_dUpzRWhDRXNwdWc");
  var childFolders = parentFolder.getFolders();
  while(childFolders.hasNext()) {
    var child = childFolders.next();
    Logger.log(child.getName());
    getSubFolders(child);
  }
}
4
votes

This Google Script will generate a Google Drive Tree with all the files and folders.

Copy this script to your script editor. Select the function "genFolderTree" from the "Select function" menu. Change the root folder name 'RootDir' to your root directory in the function genFolderTree()

var foldername = 'RootDir';

The Google Script source code -

function genFolderTree() {

  try {

  var foldername = 'RootDir';
  var folderlisting = 'TreeView_' + foldername;

  var parentFolder = DriveApp.getFoldersByName(foldername).next();


  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  var frontCell = [];
  sheet.appendRow([foldername]).getCurrentCell().setFontWeight('bold').setFontColor('red');
  frontCell.push(" ");
  getChildNode(parentFolder,sheet,frontCell);
  var files = parentFolder.getFiles();
  while (files.hasNext()) {
    frontCell.push(files.next().getName());
    sheet.appendRow( frontCell);
    frontCell.pop();
    }

  } catch (e) {

    Logger.log(e.toString());

  }

}


function getChildNode(parent,sheet,frontCell) {

  var childFolders = parent.getFolders();
  while (childFolders.hasNext()) {

    var childFolder = childFolders.next();

    frontCell.push(childFolder.getName())
    sheet.appendRow(frontCell);
    sheet.getRange(sheet.getLastRow(), frontCell.length).setFontWeight('bold').setFontColor('red');
    frontCell.pop();
    var files = childFolder.getFiles();
    frontCell.push(" ");
    var start_row = 0;
    var row_no = 0;
    while (files.hasNext()) {
      frontCell.push(files.next().getName());
      sheet.appendRow(frontCell);
      if(row_no==0){
        start_row = sheet.getLastRow();
      }
      row_no=row_no+1;
      frontCell.pop();
    }
    if(row_no>0){
      var range;
      range = sheet.getRange(start_row, frontCell.length,row_no);
      // The row grouping depth is increased by row_no.
      range.shiftRowGroupDepth(1);
    }

    // Recursive call for any sub-folders
    getChildNode(childFolder,sheet,frontCell);
    frontCell.pop();
  }

}

You can download the source from Github link

1
votes

This should do the trick:

function start() {
 folder = DriveApp.getFolderById("0B_5HSTQXtXmsOHBzcnc2dTlkRFU") 
 listFolders(folder)
}

function listFolders(folder) {
  //starting point, we come here from start() or from the bottom of this function



  //check the name
  var name = folder.getName();

  if (name.match("TITL")) { 
     folder.addEditor("[email protected]"); 
  }

  //now see if this folder has subfolders
  var subfolders = folder.getFolders();

  //if it has, we call this function again
  while (subfolders.hasNext()) {    
    listFolders(subfolders.next());
  }

}
0
votes

Think this approach may be less duplicative. This outputs a full path name in the format:

'/Root/Directory/Directory/Directory/...'

Where the 'Logger' line is, you have access to the current folder object (callingFolder) so you can insert any action on that folder at this point.

function myFunction() {
  var topFolder = DriveApp.getFolderById("0AMBwhqwkCWBbUk9PVA");
  var topPath = "/";
  iterateSubFolders(topFolder, topPath);
}

function iterateSubFolders(callingFolder, callingPath) {

  var callingFolderName = callingFolder.getName();
  var callingFolderFullPath = callingPath + callingFolderName + "/";
  Logger.log(callingFolderFullPath);

  var childSubFolders = callingFolder.getFolders();
  while (childSubFolders.hasNext()) {
      var nextSubFolder = childSubFolders.next();
      iterateSubFolders(nextSubFolder, callingFolderFullPath);
  }
}