如何解决更快地获取文件夹中最近上传的文件
我使用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;
}
该程序运行良好,但是当有很多文件夹时,当然会花费一些时间。 我想使速度更快,但这有可能吗?我尝试了没有递归的方法,但是它使代码更长且更丑陋,所以我认为我会坚持使用递归。但是我不确定这是否是导致整个过程变慢的主要原因。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。