在我们迁移到Team Drives期间,我们有一个迁移了两次的文件夹。第一次被打断了,我们现在有数以千计的重复文件夹,里面没有任何内容。我想删除或至少删除特定团队驱动器上完全为空的任何文件夹。我找到了一个旧脚本,我试着在下面修改:
function delEmptyFolders() {
var folders = DriveApp.getFolders();
var ss = SpreadsheetApp.create("folderslist");
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFiles();
var childfolders = folder.getFolders();
var filecount = 0;
var foldercount = 0;
while (childfolders.hasNext()){
var childfolder = childfolders.next();
foldercount ++;
}
while (files.hasNext()){
var file = files.next();
filecount ++;
}
ss.appendRow([folder.getId(),folder.getName(),filecount, foldercount]);
if (filecount == 0 && foldercount == 0){
folder.setTrashed(true);
}
}
}
我的问题是如何让这个脚本在特定的团队驱动器上运行,或者更好的是,在团队驱动器的特定文件夹中运行?
原始问题在评论中回答 - 下面的新脚本 - 评论中的后续问题
提前感谢您的帮助和道歉,因为我在编码或脚本方面经验不足。
function starEmptyFolders() {
var folders = DriveApp.getFolderById('ID').getFolders();
var ss = SpreadsheetApp.create("test");
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFiles();
var childfolders = folder.getFolders();
var filecount = 0;
var foldercount = 0;
while (childfolders.hasNext()){
var childfolder = childfolders.next();
foldercount ++;
}
while (files.hasNext()){
var file = files.next();
filecount ++;
}
////////////////////
var child = childfolders;
var subFolders = child.getFolders();
while(subFolders.hasNext()) {
var subFolder = subFolders.next();
var subfoldercount = 0;
subfoldercount ++}
///////////////////////////
}
ss.appendRow([folder.getId(),folder.getName(),filecount, foldercount, subfoldercount,]);
if (filecount == 0 && foldercount == 0){
folder.setStarred(true);
答案 0 :(得分:1)
这是修改后的功能,它将递归删除空文件夹并将其记录到电子表格中。
function deleteEmptyFolders() {
var parentFolder = DriveApp.getFolderById('<folder ID goes here>');
var folders = parentFolder.getFolders();
var ss = SpreadsheetApp.create("ScriptDeletes");
while (folders.hasNext()) {
var childfolder = folders.next();
recurseFolder(parentFolder, childfolder, ss);
}
}
function recurseFolder(parentFolder, folder, ss) {
var filecount = 0;
var foldercount = 0;
var files = folder.getFiles(); // get the list of files in the root of that folder
var childfolders = folder.getFolders(); // get the list of sub folders in the root of that folder
while (childfolders.hasNext()) { // count the sub folders
var childfolder = childfolders.next();
recurseFolder(folder, childfolder, ss);
foldercount++;
}
while (files.hasNext()) { // count the files in the root of the folder
var file = files.next();
filecount++;
}
if (filecount == 0 && foldercount == 0) {
parentFolder.removeFolder(folder);
ss.appendRow([folder.getId(),folder.getName(),filecount, foldercount]);
}
}
答案 1 :(得分:0)
我对@Brett的答案做了一些修改,因为它以自上而下的顺序而不是自下而上的顺序遍历目录树。我有很多嵌套的空子目录,想删除它们,所以对我来说,最好在递归后重新检查filecount / foldercount ,以便考虑更深层次的删除。我还通过不关心文件计数或文件夹计数来加快速度,即使是否存在文件
此外,如果您要删除文件夹,我认为最好使用.setTrashed
以便将其丢弃,而不是.removeFolder
那样将其移动到隐藏的顶级目录中
function deleteEmptyFolders() {
var parentFolder = DriveApp.getFolderById('FOLDER_ID');
var folders = parentFolder.getFolders();
var ss = SpreadsheetApp.openById("SPREADSHEET_ID")
while (folders.hasNext()) {
var childfolder = folders.next();
recurseFolder(parentFolder, childfolder, ss);
}
}
function recurseFolder(parentFolder, folder, ss) {
var childfolders = folder.getFolders(); // get the list of sub folders in the root of that folder
while (childfolders.hasNext()) { // count the sub folders
var childfolder = childfolders.next();
recurseFolder(folder, childfolder, ss);
}
var hasFile = folder.getFiles().hasNext(); // get the list of files in the root of that folder
var hasFolder = folder.getFolders().hasNext(); // get the list of files in the root of that folder
if (!hasFile && !hasFolder) {
folder.setTrashed(true)
ss.appendRow([folder.getId(),folder.getName()]);
}
}