Google apps script - iterate folder and subfolder
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);
}
}
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
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());
}
}