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());
}
}
Once refreshed, you will see an option labeled “List File/Folders” in the toolbar. Click on it and choose List All Files and Folders from the menu.
FolderIterator. An object that allows scripts to iterate over a potentially large collection of folders. Folder iterators can be accessed from DriveApp , a File , or a Folder . // Log the name of every folder in the user's Drive.
You can create folders and subfolders for all of your files so each one is in a specific place. By naming and adding color to your folders, you can quickly find your files when you need them. To begin, open your Google Drive. Then, create and name a new folder.
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
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);
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With