我正在尝试创建一个脚本来为我的Google驱动器中的所有内容打印完整文件路径的列表。目的是要有一个这样的列表:
./Docs/Doc1.gdoc
./Docs/Doc2.gdoc
./Docs/Doc3.gdoc
./Pics/2011/img1.jpg
./Pics/2011/img2.jpg
./Pics/2011/img3.jpg
...
看来这并不像我想的那么简单,因为getFolders()
之类的Google云端硬盘方法将所有文件夹都视为子文件夹。
我尝试写一些递归的方法来做到这一点,直到意识到子文件夹的问题:
function getChildFolders(parent) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var pathString = childFolder.getName()
var files = childFolder.getFiles();
while (files.hasNext()) {
var fullPath = parent.getName()
var fileName = files.next().getName()
fullFilePath = pathString.concat('/', fileName)
Logger.log(fullFilePath);
}
// Recursive call
getChildFolders(childFolder);
}
}
我无法找出以这种方式以分层方式列出文件的方法。有没有人能够做到这一点?
这应该转储您要查找的内容:
function dumpFilesInFolder(folder, path)
{
path = (path || "") + "/" + folder.getName();
Logger.log(path);
var subFolders = folder.getFolders();
while(subFolders.hasNext())
{
dumpFilesInFolder(subFolders.next(), path);
}
var files = folder.getFiles();
while(files.hasNext())
{
Logger.log(path + "/" + files.next().getName());
}
}
function startIt()
{
dumpFilesInFolder(DriveApp.getRootFolder());
}