I need to list all files and folders of google drive up to some level. The code below is listing all the files and their folders (until it exceeds its time limit) and logs them. How to I add some way to stop if it recursively went, let´s say, 3 levels of subfolders?
function listFolders(folder) {
folder = folder || DriveApp.getRootFolder();
var folderName = folder.getName();
var files = folder.getFiles();
while (files.hasNext()) {
var fileName = files.next().getName();
Logger.log(folderName + " :: " + fileName);
}
var subfolders = folder.getFolders();
while (subfolders.hasNext()) {
listFolders(subfolders.next());
}
}
How about this modification? I think that there are several answers for your situation. So please think of this as just one of them.
Modified script:
When you use this modified script, please run
main()
.Note:
If I misunderstood your question, please tell me. I would like to modify it.