问题描述
我需要列出Google驱动器的所有文件和文件夹到某个级别.下面的代码列出了所有文件及其文件夹(直到超过其时间限制)并记录了它们.如果递归进行了,我该如何添加某种停止方式,比如说3个级别的子文件夹?
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.
使用此修改后的脚本时,请运行main()
.
When you use this modified script, please run main()
.
function main() { // Added
const folder = // Please set here.
const n = 3; // Please set here. This sample sets 3 as your question.
listFolders(folder, n);
}
function listFolders(folder, n) { // Modified
folder = folder || DriveApp.getRootFolder();
var folderName = folder.getName();
var files = folder.getFiles();
while (files.hasNext()) {
var fileName = files.next().getName();
Logger.log(folderName + " :: " + fileName);
}
if (--n == 0) return; // Added
var subfolders = folder.getFolders();
while (subfolders.hasNext()) {
listFolders(subfolders.next(), n); // Modified
}
}
注意:
- 您的脚本是Google Apps脚本.因此,我将您的脚本修改为Google Apps脚本.
- 请根据您的情况对其进行修改.
- Your script is Google Apps Script. So I modified your script as Google Apps Script.
- Please modify it for your situation.
Note:
如果我误解了您的问题,请告诉我.我想修改它.
If I misunderstood your question, please tell me. I would like to modify it.
这篇关于如何在javascript中递归列出N级以下的Google云端硬盘文件和文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!