我使用Google Apps脚本(GAS)编写了一个程序,用于检查(父)文件夹,并查找其中过去30分钟内上载的所有文件。我为此使用递归,因为我不知道该父文件夹中有多少个文件夹,每个文件夹有多深,所以我必须检测其中的所有文件。
var arrRecentFiles = [];
var now = new Date();
function FindRecentlyUploadedFiles(pFolder)
{
var cFolders = pFolder.getFolders();
while(cFolders.hasNext())
{
var cFolder = cFolders.next();
var cFiles = cFolder.getFiles();
while(cFiles.hasNext())
{
var cFile = cFiles.next();
//Push only if the file was uploaded within the past 30 minutes
if( CheckIfRecent(cFile) )
{
arrRecentFiles.push(cfile.getName());
}
}
//Recursion here
FindRecentlyUploadedFiles(cFolder);
}
}
/*Check if this cFile was uploaded within the past 30 minutes */
function CheckIfRecent(cFile)
{
var diff = (now.getTime() - cFile.getDateCreated().getTime()) / (60 * 1000);
if(diff < 30)
return true;
else
return false;
}
该程序运行良好,但是当有很多文件夹时,当然会花费一些时间。 我想使速度更快,但这有可能吗?我尝试了没有递归的方法,但是它使代码更长且更丑陋,所以我认为我会坚持使用递归。但是我不确定这是否是导致整个过程变慢的主要原因。