Skip to content

Instantly share code, notes, and snippets.

@mtfurlan
Created February 14, 2021 22:41
Show Gist options
  • Save mtfurlan/1a9fe884df7982bab81fa04a4949f53d to your computer and use it in GitHub Desktop.
Save mtfurlan/1a9fe884df7982bab81fa04a4949f53d to your computer and use it in GitHub Desktop.
List out all files in a google drive folder
/**
* List out all files in a google drive to a spreadsheet.
* You have to modify the filter stuff at line 15 because it will
* timeout eventually before it's done, so you have to target what you list.
*
* When you download the spreadsheet as csv it has \r\n
*
* cat file.csv | dos2unix | xargs -d '\n' -L1 rm -f
**/
function recursivePrintToSheet(folder, parentPath, sheet) {
var path = parentPath + "/" + folder.getName();
console.log("called on " + folder.getName());
console.log("path " + path);
if (folder.getName().includes("whatever") || folder.getName().includes("stuff")) {
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var filePath = path + "/" + file.getName();
console.log(filePath);
sheet.appendRow([filePath]);
}
}
var folders = folder.getFolders();
while (folders.hasNext()) {
var newFolder = folders.next();
recursivePrintToSheet(newFolder, path, sheet);
}
}
function runMe() {
var folderlisting = 'listing of folder';
var folderID = 'XXX';
var folder = DriveApp.getFolderById(folderID);
var ss = SpreadsheetApp.create(folderlisting);
var sheet = ss.getActiveSheet();
recursivePrintToSheet(folder, ".", sheet);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment