Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Apps Script to recursively list files in a folder
/*
Recusrsively add a list of files from a named foler to a sheet
*/
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var searchMenuEntries = [{
name: "Create List from Folder",
functionName: "start"
}];
ss.addMenu("Document List", searchMenuEntries);
}
function start() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type", "Folder", "Folder Slug"]);
var folderName = Browser.inputBox("Enter Folder Name (this will destroy the current spreadsheet):");
//var folderName = "Safety Subdomain Assets";
var folder = DriveApp.getFoldersByName(folderName);
if (folder.hasNext()) {
processFolder(folder);
} else {
Browser.msgBox('Folder not found!');
}
function processFolder(folder) {
while (folder.hasNext()) {
var f = folder.next();
var contents = f.getFiles();
addFilesToSheet(contents, f);
var subFolder = f.getFolders();
processFolder(subFolder);
}
}
function addFilesToSheet(files, folder) {
var data;
var folderName = folder.getName();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
sheet.appendRow([
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
file.getMimeType(),
folderName
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment