Skip to content

Instantly share code, notes, and snippets.

@PovilasID
Forked from sparkalow/list-files-in-folders.gs
Last active September 26, 2022 19:58
Show Gist options
  • Save PovilasID/ec4f5115a407b9ae3671d365b012b3a5 to your computer and use it in GitHub Desktop.
Save PovilasID/ec4f5115a407b9ae3671d365b012b3a5 to your computer and use it in GitHub Desktop.
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", "Thumbnail"]);
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([
'=HYPERLINK("'+file.getUrl()+'", "' +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,
//yourImageURL&sz=w{{width}}-h{{height}} setting size
'=IMAGE("https://drive.google.com/thumbnail?id='+file.getId()+'")'
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment