Skip to content

Instantly share code, notes, and snippets.

@AhmedThahir
Forked from sparkalow/list-files-in-folders.gs
Last active March 14, 2024 11:42
Show Gist options
  • Save AhmedThahir/89b517d50c7ded7e625d6578c56556dd to your computer and use it in GitHub Desktop.
Save AhmedThahir/89b517d50c7ded7e625d6578c56556dd 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 main() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var searchMenuEntries = [{
name: "List Folder Content Recursively",
functionName: "start"
}];
ss.addMenu("List Folder Content Recursively", searchMenuEntries);
}
function list_folders_recursively() {
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
]);
}
}
SpreadsheetApp.getUi().alert('Completed!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment