# Google Drive folders properties caching to file

I have the recursive function below to cache all the folders of my company Drive into a file. It works like a charm but takes too long, specifically >30 secs to read and save the 300 folders.

What I need is to make it faster somehow. I don't know if it is possible to make the folder reading asynchronous, or if it possible to make a webhook or something to listen to changes in the Drive's folders, which would eliminate the need to run recursively over all folders.

function generateFolderTree( idParent, tree ){
var currParent = DriveApp.getFolderById( idParent),
folderChildren = currParent.getFolders(),
currChild,
currChildId;

if( tree.length == 0 ){
tree.push({ 'text' : currParent.getName(), 'parent' : '#', 'id' : idParent, 'state' : { 'opened' : true }, 'li_attr':{'title': ''} });
}

while( folderChildren.hasNext() ){
currChild = folderChildren.next();
currChildId = currChild.getId();

tree.push({ 'text' : currChild.getName(), 'parent' : idParent, 'id' : currChildId, 'li_attr':{'title': currChild.getDescription()} });

generateFolderTree( currChildId, tree );
}
}

function geneteAndSaveTheTree(){
var theTree = [];

generateFolderTree(folderToIterateId, theTree);

DriveApp.getFileById(theFileWhereAllFoldersAreStored).setContent(JSON.stringify(theTree));
return theTree;
}


I know you can get all folders at once for a Drive, but I think it would take even longer to find out all the folders that are child of the folderToIterateId.

• You have not explicitly handled the case of an invalid folder ID parameter in gerarJqTree().